Search code examples
pythonclassstatic-variables

Will a class variable in python be declared again when a new instance is created?


I'm practicing the following code:

class MITPerson(Person): # a subclass of class Person

    nextIdNum = 0 # identification number

    def __init__(self, name):
        Person.__init__(self, name)
        self.idNum = MITPerson.nextIdNum 
        MITPerson.nextIdNum += 1 

    def getIdNum(self):
        return self.idNum

For a reference, here is the superclass:

class Person(object):  # superclass

    def __init__(self, name):
        """Create a person"""
        self.name = name

I thought that I've already known the answer of this question since I try the following instances:

p1 = MITPerson('Mark Guttag')
p2 = MITPerson('Billy Bob Beaver')
p3 = MITPerson('Billy Bob Beaver')

Not surprisingly, when I type these into console:

In[12]: p1.getIdNum()

Out[12]: 0

In[13]: p3.getIdNum()

Out[13]: 2

I've read this post and checked all the excellent answers here: Static class variables in Python

I saw that nextIdNum is assigned to 0 when the first instance p1 is created. What I feel weird is that why not p2 and p3 also bind nextIdNum to 0 again? In my imagination, the class variable should be reassigned to 0 once a class MITPerson is created without calling the method.

Did I miss something?

By the way, I've also go through the tutorial here: https://docs.python.org/3.6/tutorial/classes.html#class-objects

However, I'm afraid it does not give out the answer :(


Solution

  • I saw that nextIdNum is assigned to 0 when the first instance p1 is created.

    This is wrong. nextIdNum is assigned to 0 when the class is defined.