Like their counterparts in other languages, cla^es in Python are collections orfvariables and the methods that operate on them. To iUustrate, here's an example class, Car, that inSerto from the two othar classes,
Automobile and Transportation:
»> class Transportation:
>>> class Car(Automobile, Transportation):
... print "similar to a destructor"
We can create an inslance of Car and call it car.
>>> car = Car("Chevy", "Nova") #Create an instance of car
Make Chevy, Model Nova
And we can show that car is also an instance of classes Car and Automobi le.
>>> print isinstance(car, Automobile) 1
Note that Car defines two methods:_init_, which acts as a constructor; and__del_, which acts as a destructor._str__returns a string representation of the class.
Unlike Java classes, Python classes can have multiple inheritance, which we saw above with Car inheriting functionality from both Automobile and Transportation. In this regard, Python is more like C++ and Smalltalk.
Chapters 5 and 6 cover Python classes. Chapter 9 covers built-in operations and functions that pertain to classes.
Was this article helpful?