Classes are ways of grouping data together so that they can be manipulated in code as a single unit. Imagine various rooms in a house – bedroom, hall, kitchen, dining room, bathroom. The rooms have individual functionality but they can be grouped together into a single unit called a house. Often in programming you need ways to group data together so that it becomes easier to do calculations or implement logic. Grouping data together is called creating a Class. So in this example, we define a Class called House.
A Class contains two things – methods and class variables.
Methods are functions which are declared inside the class. For example a BankAccount class can have methods called deposit() and withdraw().
The difference between a normal variable and a class variable is that of visibility. A class variable is available all across the class and all methods in the class can access them. They are also known as Attributes.
Once a class is defined we can use it by calling it as a function. All attributes and methods in a class can be accessed by prefixing it with the object name.
You will notice that when we define a method we put self as a parameter, though its not used when the method is called from the object. Python needs a reference to the object passed to its own methods. The self parameter takes care of that.
print "area=", self.length * self.width, "sq ft."
You will see that the constructor has three parameters defined apart from self. We have also set up default values for each of them so that the default empty constructor as in kitchen = Room() still works. This is not necessary of course. If you want that the constructor should always have the parameters set then you can remove the default values.
We define a class called House which contains Rooms as a list . Every time we use the addRoom() it adds a Room object to the list.
You can see the advantages of using classes in the example above. It enables us to structure and organise data very efficiently.
In other languages you have the concept of visibility and scope for methods and attributes. For example n C++ and Java you set a method as public,private or protected. Each of these qualifiers sets the visibility of the method. So a public method can be accessed from outside the class, private methods can only be accessed within the class and protected methods are visible only to a subclass (More on that in the next section).
The same goes for attribute variables. But in Python, everything in a class has a public scope. If you still want to designate a method or attribute as private then name it with ___ eg, __mymethod__() . This is just a visible hint to other people working with your code that you do not intend that method to be accessed from outside the class. In certain ways, Python will block access to the method from outside the class because it mangles the method name starting with ___ to something else, but you can still work your way around this restriction. Generally in Python, it is a matter of culture to not directly access any class method or attribute which is surrounded by ___ and ___.
Lets see how to make subclasses now. A subclass is a Class which inherits another Class. The reason for doing so would be to extend the functionality of the class without duplicating code or losing existing classes. So in the example above, we can define a subclass of House called Apartment and another called Bungalow. These two classes will inherit all the data of House and then add their own methods and attributes. Creating subclasses helps us to build a hierarchy of classes which are all connected to each other.
print "Animal has been initialized"
print self.species ," is walking"
print self.species, " is running"
print "No species has been set"
print "Pet has been initialized"
print self.owner, " is the owner"
A new method which is used above is the __del__() method. Just like __init__() allows us to set our own constructor __del__() lets us setup our own destructor. The destructor method is called when the class is about to be cleared from memory by Python.
Pet is setup as a subclass of Animal. It automatically inherits all the attributes and methods of Animal. It can even change the functionality of methods in the parent class. So the domesticate() method has been redefined in the Pet class.
For class inheritance to work properly, the subclass must call the corresponding methods of the parent class as required. This is done by calling the method with the parent class name prefix. In the __init__() constructor of the Pet class, we call the constructor of Animal also. Likewise in the domesticate() method in the Pet class, we call the domesticate() method of the Animal class. This ensures that the parent class is getting correctly updated in sync with the subclass.
The isinstance() function is a built in function which checks the class type of an object. So we see that rabbit is of Animal class, pet is an Animal class also since it inherits the Animal class via the Pet class. The rabbit object is not a Pet class type, since it was created as an Animal class.
In the next post , we will look at the basics of working with MySQL databases.