Python OOP
Python OOP
Electrical Engineering
(E.E)
Semester 4
June 5, 2024
Outline
A class is a type :
I That is more complex than just a number or a character.
An object is a collection of :
I Member data (or attributes)
I The methods that manipulate the member data.
1 class MaClass :
2 ’’’ Brief class documentation ’’’
1 class MaClass :
2 ’’’ Brief class documentation ’’’
3 def fonctionMembre ( self ) :
4 print ( ’ Hello ’)
1.3. Object
You can create an instance (object) of a class by using the
name of the class followed by brackets
1 # affiche ’ Hello ’
2 oneInstance . fonctionMembre ()
1.5. Constructor
Among the special member functions, there is the constructor
of the
1 class NombreComplexe :
2 def __init__ ( self ) :
3 self . Re = 0
4 self . Im = 0
1 class NombreComplexe :
2 def __init__ ( self , r , i ) :
3 self . Re = r
4 self . Im = i
1.6. Destructor
One of the special functions is the class destructor.
It has the following name: __del__
when you request the destruction yourself : del obj
2.1. Inheritance
Inheritance is used to transfer member data and functions
from one (parent) class to another (child) class.
If a class B inherits from a class A, instances of class B
will have access to the members of A plus those specific to
B.
A child class can also redefine member functions inherited
from the parent class.
2.6. TP suite
3. Definition
Encapsulation is a mechanism that consists of gathering data
and methods within a class, while hiding the object’s
implementation.
The principle of encapsulation allows objects to be presented
in two possible views:
I the external view
I the internal view:
To implement the principle of encapsulation, OOP languages
introduce and use the notion of visibility.
3. Encapsulation in Python
To implement the principle of encapsulation, object-oriented
programming languages introduce and use the notion of
visibility.
There are 3 types of visibility: public, protected, private
Rule:
I Attributes must be private
I The operations defining the interface of a class must be
public (services offered)
I Operations can also be private if they only participate
in the internal functioning of the class
1 class Stagiaire :
2 def __init__ ( self , numi , age , nom , prenom , note1 , note2 ) :
3 self . numinscription = numi
4 self . __age = age
5 self . __nom = nom
6 self . __prenom = prenom
7 self . __note1 = note1
8 self . __note2 = note2
1 class Person :
2 def __init__ ( self , numi , age , nom , prenom ) :
3 self . _numinscription = numi
4 self . _age = age
5 self . _nom = nom
6 self . _prenom = prenom
Raspberry Pi
Raspberry Pi
The Raspberry Pi is a credit-card-sized, single-board,
ARM-based nanocomputer designed by professors in the Computer
Science Department at Cambridge University as part of the
Raspberry Pi Foundation.