Python Classes
On this page
Classes
Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
Class Definition
The simplest form of class definition looks like this:
class ClassName:pass
Class definitions, like function definitions (def
statements) must be executed before they have any effect.
Class Objects
Class objects support two kinds of operations: attribute references and instantiation.
class MyClass:"""A simple example class"""i = 12345def f(self):return 'hello world'
MyClass.i
and MyClass.f
are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of MyClass.i
by assignment. __doc__
is also a valid attribute, returning the docstring belonging to the class: "A simple example class"
.
Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example (assuming the above class):
x = MyClass()
creates a new instance of the class and assigns this object to the local variable x
.
The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__()
, like this:
def __init__(self):self.data = []
When a class defines an __init__()
method, class instantiation automatically invokes __init__()
for the newly created class instance.
Instance Objects
Now what can we do with instance objects? The only operations understood by instance objects are attribute references. There are two kinds of valid attribute names: data attributes and methods.
For example, if x
is the instance of MyClass
created above, the following piece of code will print the value 16
, without leaving a trace:
x.counter = 1while x.counter < 10:x.counter = x.counter * 2print(x.counter)del x.counter
Method Objects
Usually, a method is called right after it is bound:
x.f()
In the MyClass
example, this will return the string 'hello world'
. However, it is not necessary to call a method right away: x.f
is a method object, and can be stored away and called at a later time. For example:
xf = x.fwhile True:print(xf())
will continue to print hello world
until the end of time.
Class and Instance Variables
Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:
class Dog:kind = 'canine' # class variable shared by all instancesdef __init__(self, name):self.name = name # instance variable unique to each instance>>> d = Dog('Fido')>>> e = Dog('Buddy')>>> d.kind # shared by all dogs'canine'>>> e.kind # shared by all dogs'canine'>>> d.name # unique to d'Fido'>>> e.name # unique to e'Buddy'
Inheritance
Language feature would not be worthy of the name “class” without supporting inheritance. The syntax for a derived class definition looks like this:
class DerivedClassName(modname.BaseClassName):pass
Multiple Inheritance
Python supports a form of multiple inheritance as well. A class definition with multiple base classes looks like this:
class DerivedClassName(Base1, Base2, Base3):pass