this may be a silly question. when we declare a member variable, why we don't do it in init and why we don't need to use self.member_variable to specify which class this variable belongs to? put it another way, why the variables in init() have to have self. in front of them. Thank you.
A member variable is one that applies to the entire class, rather to a particular instance of that class. So, at any given time, there is only one occurrence of that variable, regardless of whether there are zero instances of the class or a thousand instances of the class. In contrast, an instance variable is one where there is a separate occurrence of the variable for each instance of the class, with each occurrence of the instance variable capable of having its own value.
The purpose of an
__init__ method is to set up a particular instance of the class, which typically involves initializing the values of variables that apply specifically to that instance. However, an
__init__ method can perform other tasks as well, and that can include altering the value of a class member variable. For example, a member variable named
count might be assigned the value
0 in the class definition, and the
__init__ method might add
1 to whatever is the current value of
count would represent the number of instances of the class.
So, we initialize member variables outside the
__init__ method, because their scope lies outside the scope of a particular instance. We use
self to specify that we are referring to instance variables, in order to distinguish accessing those variables from accessing member variables or temporary local variables. It is not obligatory that we use the name
self to refer to the instance, but it is customary to use that name. Whatever name we do use for the first variable in the parameter list of a method is the name that will refer to the current instance within that method.
hi, @appylpye, thank you very much for the thorough explaination. Much appreciated.
count example, if i declared this member variable as you said (it doesn't apply to a particular instance), will I be able to access it without instantiating the class? or this member variable is only accessible to the class' instance?
Hi, @oceortsui ,
Glad you asked. Here's an example that you can copy for experimentation.
Within the code, note that
@staticmethod is a decorator that enables the
reveal_count method to refer to the
Egg class on which it is called.
# Egg.py # October 20, 2015 class Egg(object): count = 0 # Member variable to keep count of Egg instances def __init__(self, bird_type): """Instantiates an Egg; Increments the count""" self.bird_type = bird_type Egg.count += 1 # Increment count of Egg instances @staticmethod # Decorator to make reveal_count a class method def reveal_count(): """Outputs count of Egg instances""" print "We have %s eggs!" % (Egg.count) Egg.reveal_count() parrot_egg = Egg("Norwegian Blue") swallow_egg = Egg("Barn Swallow") Egg.reveal_count() ostrich_egg = Egg("Big Bird") Egg.reveal_count()
We have 0 eggs! We have 2 eggs! We have 3 eggs!