Special Class Methods

In Python you can define special methods for classes that have special meaning. You can also define methods to make your class look like other objects such as lists, dictionaries, and tuples.

Creating and Destroying Class Instances

Like other OOP languages, Python has a destructor and a constructor. A constructor is called when an instance is instantiated. A destructor is called when an instance is garbage-collected. The method __init__ denotes the constructor. The method __del__ denotes the destructor.

Here's the Car class (Car2.py) with its constructor and destructor:

 class Car: make = "?" model = "?" color = "?" running = 0 #1 is true, 0 is false def __init__(self, make, model, color): self.make = make self.model = model ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.