Python Classes
Jakob Jenkov |
Python classes are units of code into which you can group variables and functions together. A class in Python is a template for objects instantiated of that class. Thus, you can say that a class is a definition or blueprint for how objects of that class should look - when created (instantiated).
Here is an example of a Python class:
class MyPoint: def __init__(self, x, y): self.x = x self.y = y
I will go into more detail with how to define classes in Python throughout the rest of this tutorial.
Declaring a Python Class
You declare a Python class via the class keyword, like this:
class MyPoint:
This example declares a class called MyPoint - but declare nothing inside the class. We will see how to add contructors, member variables and functions to the class declaration later.
Declaring a Constructor
A constructor is a special function that is called when you create an instance (object) of a given class. The constructor is typically used to initialize the internal state of the object, meaning the values of any member variables (AKA fields or properties).
You declare a constructor in a Python class using the
__init(self)
declaration.
Here is an example of a Python class with a constructor
declared:
class MyPoint: def __init__(self, x, y): self.x = x self.y = y
This example declares a Python class called MyPoint with a constructor that takes 2 parameters named "x" and "y". The parameter "self" is a reference to the object itself, and is provided at runtime by the Python interpreter.
The constructor initializes the two member variables
self.x
and self.y
to the values provided
by the parameters x and y. These initializations also declares
the existence of these member variables at the same time.
Creating a Python Object
You create an object of a class in Python by writing the name of the class followed by parentheses. Here is an example of creating an object of the MyPoint class:
my_point = MyPoint(10, 20)
Notice how only the x and y parameters are passed to the constructor. We do not actively pass the "self" parameter to the constructor. The Python interpreter does that for us.
Referencing Member Variables
You can reference a member variable of a Python object via the variable that references the object. You can both read and write member variables. Here are some examples of both reading and writing member variables of a Python object:
my_point = MyPoint(10, 20) my_point.x = 25 my_point.y = 50 xx = my_point.x yy = my_point.y print(my_point.x) print(my_point.y)
The second and third lines of code shows how to assign new values to the x and y member variables of the object referenced by the my_point variable.
The fourth and fifth lines show how to read the values of the x and y member variables. The 6th and 7th line actually shows that too, but prints their values to the console rather than simply reading their values.
Declaring Functions
You can declare a function inside a Python class similarly to how you declare a function outside of a class. You can declare both functions that belong to the class, and functions that belong to each object of that class. I will cover both of them in the following sections:
Class Functions
Let us first see how to declare a function that belongs to a Python class:
class AnotherClass: def a_class_function(param1, param2): print("This is a class function", param1, param2)
Calling a class function is done by writing the name of the class, followed by a . and then followed by the name of the function. Here is an example of calling a Python class method:
AnotherClass.a_class_function("p1", "p2")
Object Functions
Object functions, sometimes also referred to as member functions or methods, are functions that belong to an object. Thus, when you call a function on an object, that function has access to the internal state of that object.
The access to the internal state happens via the first parameter of a member function - which is always a reference to the object the function belongs to.
Here is an example of defining a member function called area() inside a Python class:
class MyRectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height
Here is an example of calling the area() function inside of an object of the MyRectangle Python class:
my_point = MyRectangle(10, 20) area = my_point.area()
Inheritance in Python Classes
Python classes support inheritance which means that one class can extend (specialize) another class. A class that extends another class is called a subclass of the other class. The class that the subclass inherits from (extends) is called the superclass of the subclass.
When a subclass inherits from (extends) a superclass, it inherits the fields and functions (methods) of that superclass.
You specify that a Python class should inherit from another class by adding a set of parentheses after the class name, and then listing the class(es) it should inherit from inside these parentheses. Here is an example of specifying class inheritance in Python:
class MySuperclass: __init__(self, x, y): self.x = x self.y = y def area(self): return self.x * self.y
class MySubclass(MySuperclass): __init__(self, x, y, z): self.x = x self.y = y self.z = z
The class MySubclass inherits from (extends) the class MySuperclass.
MySuperclass defines two member variables (fields / properties) which MySubclass inherits. MySuperclass also defines a public function called area() which calculates the area of the self.x and self.y member variables - as if it were a geometric calculation.
The class MySubclass inherits the x and y member variables and the area() member function. Thus, you can use the area() function, like this:
my_obj = MySubclass(100, 200, 300) area = my_obj.area() print(area)
Running this example should print out 20000 which is the result of 100 * 200.
Classes in Other Programming Languages
Classes is a common concept in many programming languages. Here are links to the tutorials about classes in programming languages covered on this website:
Tweet | |
Jakob Jenkov |