0% found this document useful (0 votes)
21 views

Python OOP

Uploaded by

Zidama A SOULAMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python OOP

Uploaded by

Zidama A SOULAMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

OOP Projects

Specific applications to electrical systems

Python Object Oriented

BURKINA INSTITUTE OF TECHNOLOGY

Electrical Engineering
(E.E)

Semester 4

June 5, 2024
Outline

1 Class and Object in Python language

2 OOP concepts and Python language

3 Specific applications to electrical systems(Raspberry Pi )

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 2 / 60


1. Class and Object in Python
language

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 3 / 60


1. Class and Object in Python language
1.1. Class - Object
In object-oriented programming (OOP), all variables are objects
associated with a class.

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.

You can create as many class objects (variables of the type


described by the class ) as you like.

An object is also called an instance of a class

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 4 / 60


1. Class and Object in Python language
1.2. Class
To define a class, use the keyword class

1 class MaClass :
2 ’’’ Brief class documentation ’’’

We can then add member data and member functions

1 class MaClass :
2 ’’’ Brief class documentation ’’’
3 def fonctionMembre ( self ) :
4 print ( ’ Hello ’)

memberfunction is a member function that can be called on an


instance of the MaClass class
(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 5 / 60
1. Class and Object in Python language

1.3. Object
You can create an instance (object) of a class by using the
name of the class followed by brackets

1 # oneInstance is a instance of class MaClass


2 oneInstance = MaClass ()

Member data and member functions can then be accessed using


the .(dot ) operator.

1 # affiche ’ Hello ’
2 oneInstance . fonctionMembre ()

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 6 / 60


1. Class and Object in Python language

1.3. Object: Reference to the instance


You will notice the presence of the self parameter in the
member function

1 def fonctionMembre ( self ) :

Within the member function, selfdefines the instance on which the


function is called, so it can be used to access member data and
member functions specific to this instance.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 7 / 60


1. Class and Object in Python language

1.3. Object: Reference to the instance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 8 / 60


1. Class and Object in Python language

1.4. Special members


Data and functions that begin with a double-down hyphen (__)
are special members, and have a special meaning.

There are several special data members, including


I __doc__ : class documentation (given by the docstring,
i.e. the text between ”’ below the class name)
I __name__ : class name
I __dict__ : dictionary where the keys are the names of
the member data and the values are the values of the
member data.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 9 / 60


1. Class and Object in Python language

1.5. Constructor
Among the special member functions, there is the constructor
of the

Its name is : __init__.

This function is called when an instance of the class is


created.

I returns the object (self ) after creation

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 10 / 60


1. Class and Object in Python language
1.5. Constructor
The constructor is usually used to initialise data values.

1 class NombreComplexe :
2 def __init__ ( self ) :
3 self . Re = 0
4 self . Im = 0

It therefore usually takes the values of the member data to


be given to this instance as parameters

1 class NombreComplexe :
2 def __init__ ( self , r , i ) :
3 self . Re = r
4 self . Im = i

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 11 / 60


1. Class and Object in Python language

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

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 12 / 60


1. Class and Object in Python language
1.7. Representation of an object
The special member function __repr__ returns the character
string to be displayed when you type the object name directly

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 13 / 60


1. Class and Object in Python language

1.7. Representation of an object


The special member function __str__ returns the character string
to be displayed when the prints function is called on the

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 14 / 60


1.OOP concepts and Python language

1.8. Operator overload

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 15 / 60


1.OOP concepts and Python language

1.8. Operator overload


This means you can overload most mathematical operations.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 16 / 60


1.OOP concepts and Python language

1.8. Operator overload


Comparison operators can also be overloaded.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 17 / 60


1.OOP concepts and Python language

1.8. Operator overload


Comparison operators can also be overloaded: example

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 18 / 60


1.OOP concepts and Python language

1.8. Operator overload


Logic operators too.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 19 / 60


1.OOP concepts and Python language

1.8. Operator overload


Logic operators too: example

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 20 / 60


2. OOP concepts and Python language

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 21 / 60


2. OOP concepts and Python language

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.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 22 / 60


2. OOP concepts and Python language

2.2. Simple inheritance


For a class to inherit the members of another class, you add
the name of the parent class in brackets behind the
declaration of the child class name

1 class Fille ( Mere ) :

For example, a Dog class will inherit from an Animal class.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 23 / 60


2. OOP concepts and Python language

2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 24 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 25 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 26 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 27 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 28 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 29 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 30 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 31 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 32 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 33 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 34 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 35 / 60


2. OOP concepts and Python language
2.2. Simple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 36 / 60


2. OOP concepts and Python language

2.3. Multiple inheritance


A class can in fact inherit from several other classes.

The members of this child class will therefore be the union


of the members of all the parent classes plus those specific
to the child class.

Simply add the names of the parent classes in brackets,


separated by commas.

1 class Fille ( Mere1 , Mere2 , ...) :

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 37 / 60


2. OOP concepts and Python language

2.3. Multiple inheritance


When calling members, we look for them in the order defined
in the brackets

First in Daughter, then in Mere1, then in Mere2 etc.

Be careful, if Mere1 inherits one or more classes, we’ll look


inside before passing to Mere2.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 38 / 60


2. OOP concepts and Python language
2.3. Multiple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 39 / 60


2. OOP concepts and Python language
2.4. A few comments
Warning! in the case of multiple inheritance, where several
classes contain the same member function name, it must be
prefix it with the name of the class to remove any
ambiguity.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 40 / 60


2. OOP concepts and Python language

2.4. A few comments


The super() function is preferred for accessing a member of
the of the parent class
Because if you change the parent class, you don’t have to
change the

Every class inherits from Python’s object class, even when


this is not explicitly stated.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 41 / 60


2. OOP concepts and Python language
2.4. A few comments
Inheritance is also widely observed, with multiple classes
that all inherit from the same parent class.
Classes such as Dog, Cat, Hen, Pig etc. will all inherit
from Animal.
Class hierarchies are often visually represented represented
in a diagram.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 42 / 60


2. OOP concepts and Python language
2.4. A few comments
Example: introducing multiple inheritance

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 43 / 60


2. OOP concepts and Python language
2.5. Summary of terminology
Class
I a type defined by the programmer, grouping together
members (class and instance data and functions)
Instance / class object
I an individual variable of a certain class
Member data or attribute
I a variable that stores data associated with an instance
Member function or method
I a function called on an instance of a class
Class inheritance
I transferring characteristics from one class to another

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 44 / 60


2. OOP concepts and Python language
2.6. TP

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 45 / 60


2. OOP concepts and Python language

2.6. TP suite

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 46 / 60


2. OOP concepts and Python language
2.6. TP correction

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 47 / 60


2. OOP concepts and Python language
2.6. TP correction

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 48 / 60


2. OOP concepts and Python language
2.6. TP correction

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 49 / 60


3. Encapsulation

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.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 50 / 60


3. Encapsulation

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

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 51 / 60


3. Encapsulation

3. Encapsulation in Python: Private attributes


To make attributes private, python requires the use of an
attribute name starting with __ (double underscore) .

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

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 52 / 60


3. Encapsulation

3. Encapsulation in Python: Public methods


Most methods must be public.
To create a public member in a Python class, simply choose a
name, without adding anything at the beginning.

1 def calculMoy ( self ) :


2 moy = ( self . __note1 + self . __note2 ) /2
3 print ( " La moyenne : " , moy )

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 53 / 60


3. Encapsulation
3. Encapsulation in Python: Protected elements
If a given class is going to be a parent class, then it must
make its attributes protected instead of private.
If it defines private attributes, then the child classes that
inherit these attributes will not be able to use them!
To define a protected element, python suggests starting its
name with a _ (underscore).

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

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 54 / 60


3. Encapsulation

3. Encapsulation in Python: Accessors and modifiers


As attributes are private, we need to create methods that
allow access (to find out the value of the attribute, to
modify this value).

Getters: methods that allow you to find out the values of


attributes.

Setters (modifiers): methods for modifying attributes.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 55 / 60


3. Encapsulation
3. Encapsulation in Python: Accessors and modifiers
1 class Stagiaire :
2 def __init__ ( self , note1 , note2 ) :
3 # rendre tous les attributs prives
4 self . __note1 = note1
5 self . __note2 = note2
6 def calculMoy ( self ) :
7 # modifier la methode pour retourner la moyenne
8 moy = ( self . __note1 + self . __note2 ) /2
9 print ( " La moyenne : " , moy )
10 def getnote1 ( self ) :
11 return self . __note1
12 def setnote1 ( self , note1 ) :
13 self . __note1 = note1
14 def getnote2 ( self ) :
15 return self . __note2
16 def setnote2 ( self , note2 ) :
17 self . __note2 = note2
(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 56 / 60
3. Encapsulation
3. Encapsulation in Python: Accessors and modifiers
We can create a trainee object with no information (empty object
not yet initialised).
Then, we’ll use the modifiers to add the s2 information:
1 # Create objects
2 s1 = Stagiaire (1122 ,21 , " Tassambedo " ," A Kader " ,15.2 ,18)
3 # create s2
4 s2 = Stagiaire ()
5 s2 . setnumi (2211)
6 s2 . setage (15)
7 s2 . setnom ( " Bance " )
8 s2 . setprenom ( " Aya " )
9 s2 . note1 (15.2)
10 s2 . setnote2 (18)

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 57 / 60


3. Specific applications to
electrical systems(Raspberry Pi)

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 58 / 60


Students Research

Raspberry Pi

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 59 / 60


Students Research

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.

The Raspberry Pi was created to democratise access to


computers and digital making.

Python is the programming language of choice for Raspberry Pi


developers.

(B.I.T/E.E, S4) OOP Projects: Electrical systems June 5, 2024 60 / 60

You might also like