To share this page click on the buttons below;
Class
A python class is defined with class keyword
class <class name>:
# class properties
# class methods
object are created with
<object>=<class name>()
Methods
a class method is defined as a function belonging to the class
class <class name>:
# class method
def <method>(self,<argument1>, ...):
# method implementation
methods always receives implicitly the self parameter (equal to C++ this), implicitly means that the self argument is available in the method implementation but it not provided when the method is called. The name self is used for convention but any other name is perfectly valid, what matters is that this implicit argument is the first one.
A property can be delete using the function del
.
Constructor
Constructors are defined with the __init__
function
class <class name>:
# constructor
def __init__(self,<argument1>, ...):
# constructor implementation
__init__
function is called when the object is created.
Scope
Method and properties are always public. There is no need for getter or setter functions. To hide a bit the properties name the property with an underscore and then provide the setter / getter functions.
Properties
The way to create (a almost private) property is by using the function property
. The function property
actually links a propriety with a setter, getter and delete method (hiding the proeprty value). To set up a property:
- name the hidden property with an undescore at the beginning
- define the functions to set, get, delete the hidden property (the one with undescore)
- define the actual visible property (without the underscore) with the function property (the function take 4 parameters: the function to set the property, the function to get the property, the function to delete the property and a string which is the docstring of the property)
- use the visible property: the functions set up by the property() function are automatically called and the hidden property is actually manipulated
Note: the use of the trailing underscore is a convention (it is not a defined language feature).
class <class name>:
# constructor
def __init__(self,<argument1>, ...):
self._<property> = <value> # creation and initialization of hidden property
def <SetProp>(self,value):
self._<property> = value
def <GetProp>(self):
return self._<property>
def <DelProp>(self):
del self._<property>
<property> = property(<SetProp>, <GetProp>, <DelProp>, <docstring>)
now if <object> = <class name>
we can use the property this way <object>.<property> = <value>
and this will call automatically the _<property>
. The very same will happen when we get or delete the property.
The very same can be achieved using decorators.
The property is defined by the getter method preceded by the decorator @property
, while the setter and deleter methods are preceded by @<property>.setter
and @<property>.deleter
.
class <class name>:
# constructor
def __init__(self,<argument1>, ...):
self._<property> = <value> # creation and initialization of hidden property
@<property>.setter
def <property>(self,value):
self._<property> = value
@property
def <property>(self):
return self._<property>
@<property>.deleter
def <DelProp>(self):
del self._<property>
Static method
- Static methods belong to the class (not to the object), but they do not receive the first implicit argument (self).
- Static methods can be created with staticmethod function or (better) using the decorator
@staticmethod
before the method definition in the class. - Static method are just a way to group functions under a class.
class <class name>:
@staticmethod
def <static_method>(<argument1>, ...):
# static method implementation
Class method
- Class methods belong to the class (not to the object) and they do receive the first implicit argument (cls) which contains the class. The use of cls as name of this first implicit argument is a convention.
- Class methods can be created with classmethod function or (better) using the decorator
@classmethod
before the method definition in the class. - Class method are used as builder method (method that are able to build and return a new object using different paramenter) because they receive the class as first implicit argument.
class <class name>:
@classmethod
def <class_method>(cls, <argument1>, ...):
# class method implementation
# usually return
return cls(<arguments>,...)
Inheritance
To derive a class from a father one:
class <class name>(<father_class>):
# class implementation
The child class will inherit properties and method from the father class.
If a father class method is re-defined in the child class, this latter will override the father implementation for the child objects.
To call or use the father method in the child override function use super()
dot the father method.
for example to override the __init__
method:
class <class name>(<father_class>):
def __init__(self, <arguments>, ...):
super().__init__(<arguments>) #this will call the <father_class> init method
To share this page click on the buttons below;