Class is a very important feature in Python, which gives Python the capability of supporting OOP. A class is declared with the keyword "class". Let's take a brief tour of it in this blog.
Noted that Jupyter Lab is used for demonstration.
A Simple Class
Let's start with a simple class, which contains only one method. After instantiated the class, we can simply call the class method.
A Class with __init__() Method
We can add an __init__() method into our class to do some initialization work while an object is created.
Python doesn't support overloading by default. So we can declare only one __init__() method. To be more precise, we can declare over one __init__() methods, but only the last one is valid.
How to overload methods? There are several ways to achieve this, for example, we can use multiple dispatch decorator, assign a default value to an argument, or use dynamic arguments etc. Please check out the reference The Right Way To Overload Methods and Operators In Python for more details.
Class Variable and Instance Variable
A class variable belongs to the class, it is shared cross all instances created from this class. On the contrary, an instance variable belongs to that particular instance.
What if we define another string class variable named by "_anothermsg"? Something interesting happens.
When object "hp" is created, "_anothermsg" is assigned with a string "Hi Python". Later in the creation of object "sy", it is given another string "Hello". However, this doesn't update "_anothermsg" in object "hp". Simply put, "_anothermsg" behaves like an instance variable.
This doesn't mean something wrong. Python is born with its own philosophy, having its conventions. Overall, Python has very loose constraints.
If we simply look at this from the perspective of pass-by-reference or pass-by-value, list is pass by reference, string is pass by value, whereas slice of a list is pass by value, meaning a copy of that slice is created.
Private Variables
Python doesn't have private keyword to declare a variable. If a variable or method is named with a leading underscore "_", it should be treated as a private part in practice.
In the meanwhile, if we define a variable preceding with double underscores "__", the variable becomes inaccessible from outside the class, as illustrated by the following example.
Of course, we can use property or getter method to access it, which are described in the next section.
In nature, a variable prefixed with "__" is replaced with "_classname__variablename" in Python, for example, "__message" is replaced with "_HiPython__message".
Property
A property is defined by using property decorator "@property". It provides access to a class or an instance variable.
Getter and Setter
First, we can implement these by using methods. As shown in the example below, "set_message()" method converts a string into uppercase and assign it to the instance variable "_message", whereas "get_message()" method returns "_messasge".
One of the merits we have a setter is that we can do some transformations before the assignment.
Inheritance
Inheritance is realized by placing the super class's name inside the parentheses when we define a sub class. Multiple inheritance is also supported by Python.
"SubPython" is derived from "HiPython" in the following example.
Can a derived class add its own __init__() method? Yes, it is doable. In the below example, the sub class declares a __init__() method that calls the super class's __init__() within it.
Polymorphism
Although it is practicable to overload operators in Python, we shed light on class method here.
For overloading, we can find workarounds even Python doesn't implement it as a default feature.
For overriding, let's have a look at the example below. SayHello() method is defined in the super class and re-defined in the derived class.
Inside SubPython.SayHello(), we can also call the super class's method through super().
super().SayHello()orsuper(SubPython, self).SayHello()
Dataclass
A dataclass is specialized in holding and representing data. It is declared by using @dataclass decorator, as a consequence, some special methods such as __init__() and __repr__() etc are automatically added into the class.
Here is a dataclass example using default __init__() and __repr__() methods.
Reference
Python Tutorial by Guido van Rossum and the Python development team
Getters and Setters: Manage Attributes in Python
No comments:
Post a Comment