Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Using Class and Object Variables

Example 11.4. Using Class and Object Variables

#!/usr/bin/python # Filename: objvar.py

class Person:
'''Represents a person.''' population = 0

def __init__(self, name):
'''Initializes the person's data.''' self.name = name
print '(Initializing %s)' % self.name

# When this person is created, he/she # adds to the population
Person.population += 1

def __del__(self):
'''I am dying.'''
print '%s says bye.' % self.name

Person.population -= 1

if Person.population == 0: print 'I am the last one.'
else:
print 'There are still %d people left.' % Person.population

def sayHi(self):
'''Greeting by the person.
Really, that's all it does.'''
print 'Hi, my name is %s.' % self.name

def howMany(self):
'''Prints the current population.'''
if Person.population == 1:

print 'I am the only person here.'
else:
print 'We have %d persons here.' % Person.population

swaroop = Person('Swaroop') swaroop.sayHi()
swaroop.howMany()

kalam = Person('Abdul Kalam') kalam.sayHi()
kalam.howMany()

swaroop.sayHi()
swaroop.howMany()

Output

$ python objvar.py
(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here. (Initializing Abdul Kalam) Hi, my name is Abdul Kalam. We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Abdul Kalam says bye.
There are still 1 people left. Swaroop says bye.
I am the last one.

How It Works

This is a long example but helps demonstrate the nature of class and object variables. Here, population belongs to thePerson class and hence is a class variable. Thename variable belongs to the object (it is assigned usingself) and hence is an object variable.

Thus, we refer to the population class variable as Person.population and not as self.population. Note that an object variable with the same name as a class variable will hide the class variable! We refer to the object variablename usingself.name notation in the methods of that object. Remember this simple difference between class and object variables.

Observe that the __init__ method is used to initialize thePerson instance with a name. In this method, we increase thepopulation count by 1 since we have one more person being added. Also observe that the values ofself.name is specific to each object which indicates the nature of object variables.

Remember, that you must refer to the variables and methods of the same object using theself variable only. This is called an attribute reference.

In this program, we also see the use of docstrings for classes as well as methods. We can access the class docstring at runtime using Person.__doc__ and the method docstring as Person.sayHi.__doc__

Just like the __init__ method, there is another special method__del__ which is called when an object is going to die i.e. it is no longer being used and is being returned to the system for reusing that piece of memory. In this method, we simply decrease thePerson.population count by 1.

The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly do this, you just have to use thedel statement which we have used in previous examples.

Note for C++/Java/C# Programmers

All class members (including the data members) are public and all the methods are virtual in Python.

 

One exception: If you use data members with names using the double underscore prefix such as __privatevar, Python uses name-mangling to effectively make it a private variable.

Thus, the convention followed is that any variable that is to be used only within the class or object should begin with an underscore and all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).

Also, note that the__del__ method is analogous to the concept of a destructor.