Uploaded by Beşiktaş Sports

Python OOP Concepts for Engineers - Lecture 9

advertisement
Programming with Python for Engineers
Lecture-9
Subject : Object-Oriented Programming Concepts
Lecturer: Hüseyin SAYIN
E-Mail : hsayin@metu.edu.tr
Coordinating Assistant : Alperen Dalkıran
Support E-Mail
: 2xx@ceng.metu.edu.tr
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
1
Objectives
• In this chapter, you will learn:
– Object-Oriented Programming
in Python:
* Class definition;
* Member functions & variables;
* Message passing;
* Encapsulation;
* Inheritance;
* Polymorphism;
* Basics of OOP in Python
Fall 2023
(OOP)
Concepts
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
2
Introduction
• Divide and conquer
– Construct a program from smaller pieces or
components
• These smaller pieces are called modules
– Each piece more manageable than the original
program
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
Object Oriented Programming
• Structured programming
- Unless a randomization is explicitly build in,
all computations are deterministic; i.e., the result is
always the same for the same input.
- The logic involved is binary; i.e., two truth values exist:
True and False.
• There is a clear distinction of actions and data.
- Actions are coded into expressions, statements and
functions.
- Data is coded into integers, floating points and
containers (strings, tuples and lists).
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
4
Object Oriented Programming
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
5
Object Oriented Programming
• Python offers itself not only as a popular
scripting language, but also supports the
object-oriented programming paradigm.
- Classes describe data and provide methods to
manipulate that data, all encompassed under a single
object.
- Classes allow for abstraction by separating concrete
implementation details from abstract representations of
data.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
6
Object Oriented Programming
• OOP is a paradigm that comes with some
properties:
- Encapsulation
- Inheritance
- Polimorphism
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
7
Object Oriented Programming
• OOP is a paradigm that comes with some
properties:
- Encapsulation:
• Combining data and functions that
manipulate that data under a concept that
we name as ‘object’
* “need-to-know” rule and “maximalprivacy” is satisfied.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
8
Object Oriented Programming
• OOP is a paradigm that comes with some
properties:
- Inheritance:
• Defining an object and then using it to create
“descendant” objects so that the descendant
objects inherit all functions and data of their
ancestors.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
9
Object Oriented Programming
• OOP is a paradigm that comes with some
properties:
- Polymorphism:
• A mechanism allowing a descendant object to
appear and function like its ancestor object when
necessary.
• Often used in Class methods, where we can
have multiple classes with the same method
name.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
10
Object Oriented Programming
Encapsulation:
• Data and actions actions are glued together in a
data-action structure → object
- “need-to-know” rule and “maximal-privacy” is
satisfied.
• An object should provide access
- Only to data and actions that are needed by
other objects and other data & actions that are not
needed should be hidden and
- Used by the object itself for its own merit.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
11
Object Oriented Programming
Encapsulation:
• Keep implementation modular and manageable.
- An object stores some data and implements
certain actions.
- Some of these are private and hidden from
other objects.
- others are public to other objects.
- The public data and actions function as the
interface of the object to the outside World.
- Objects interact with each other’s interfaces
by accessing public data and actions.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
12
Object Oriented Programming
Encapsulation:
• Means restricting external access to some of the
properties and methods we define. Some
properties and methods should not be accessible
by everyone.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
13
Object Oriented Programming
Encapsulation:
• Hiding implementation/representation details.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
14
Object Oriented Programming
Inheritance:
• The objects we are going to work with are going to
be related.
• Inheritance allows us to define a class that inherits
all the methods and properties from another class.
• Parent class is the class being inherited from, also
called base class.
• Child class is the class that inherits from another
class, also called derived class.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
15
Object Oriented Programming
Inheritance:
• The objects we are going to work with are going to
be related.
• Inheritance allows us to define a class that inherits
all the methods and properties from another class.
• Creating a Parent Class: Any class can be a
parent class, so the syntax is the same as creating
any other class.
• Creating a Child Class: o create a class that
inherits the functionality from another class, send
the parent class as a parameter when creating the
child.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
16
Object Oriented Programming
Inheritance:
• The objects we are going to work with are going to
be related.
• For example, in a drawing program, we are going to
work with shapes such as rectangles, circles,
triangles which have some common data and
actions, e.g.:
Data:
Actions:
- Position
- draw()
- Area
- move()
- Color
- rotate()
- Circumference
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
17
Object Oriented Programming
Inheritance:
• For example, in a drawing
program,
• A
class
inherits
some
variables and functions from
another one.
• Square class inherits x and
draw() from the Shape class.
• Shape: Parent class
• Square: Child class.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
18
Object Oriented Programming
Inheritance:
• What kind of data structure we use for these data
and how we implement the actions are important.
• For
example,
if
one
shape
is
using
Cartesian coordinates (x, y) for position and
another is using Polar coordinates (r, θ), a
programmer can easily make a mistake by
providing (x, y) to a shape using Polar coordinates.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
19
Object Oriented Programming
Inheritance:
• As for actions, implementing such overlapping
actions in each shape from scratch is redundant
and inefficient.
• In the case of separate implementations of
overlapping actions in each shape, we would have
to update all overlapping actions if we want to
correct an error in our implementation or switch to a
more efficient algorithm for the overlapping actions.
• Therefore, it makes sense to implement the
common functionalities in another object and reuse
them whenever needed.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
20
Object Oriented Programming
Inheritance:
• These two issues are handled in OOP via
inheritance. We place common data and
functionalities into an ancestor object
• e.g. Shape object for our example) and other
objects (Rectangle, Triangle, Circle) can inherit
(reuse) these data and definitions in their
definitions as if those data and actions were
defined in their object definitions.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
21
Object Oriented Programming
Inheritance:
• In real life entities, you can observe many similar
relations. For example:
- A Student is a Person and an Instructor is a
Person. Updating personal records of a Student is
no different than that of an Instructor.
- An DCEngine, a DieselEngine, and a
StreamEngine are all Engines. They have the same
characteristic features like horse power, torque etc.
However, DCEngine has power consumption in units
of Watt whereas DieselEngine consumption can be
measured as litres per km.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
22
Object Oriented Programming
Inheritance:
• In real life entities, you can observe many similar
relations. For example:
- In a transportation problem, a Ship, a
Cargo_Plane and a Truck are all Vehicles. They
have the same behavior of carrying a load;
however, they have different capacities, speeds,
costs and ranges.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
23
Object Oriented Programming
Inheritance:
• Assume we like to improve the forecasting
accuracy through adding radar information in our
Whether-Forecast example above. We need to get
our traditional estimate and combine it with the
radar image data.
• Instead of duplicating the traditional estimator, it is
wiser to use existing implementation and extend its
functionality with the newly introduced features.
This way, we avoid code duplication and when we
improve our traditional estimator, our new estimator
will automatically use it.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
24
Object Oriented Programming
Inheritance:
• Inheritance is a very useful and important concept
in OOP. Together with encapsulation, it improves
reusability, maintenance, and reduces redundancy.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
25
Object Oriented Programming
Polymorphism:
• The word means "many forms", and
• In programming it refers to;
- methods/functions/operators with the same
name that can be executed on many objects or
classes.
• It is a property that enables a programmer to write
functions that can operate on different data types
uniformly.
• In OOP, all descendants of a parent object can act
as objects of more than one types.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
26
Object Oriented Programming
Polymorphism:
• It is a feature of a programming language that
indicates its ability to handle different types of data
and classes differently.
• It can also be expressed as the ability to take
various forms. It is the use of an object or method
in different places in different ways.
• To be more specific is the ability to redefine
methods and derived classes.
• Shortly; two or more objects can be referenced by
the same type of object.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
27
Object Oriented Programming
Polymorphism:
• The work done by methods that are transferred
from the parent class to the child class by
inheritance but used in a different way in the child
class is polymorphism.
• In short, it is used to get different outputs by using
the same method on the name of polymorphism.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
28
Object Oriented Programming
Polymorphism:
• For example, the + operator adds numerical values
mathematically, while it performs the task of
combining text values.
• So in Python the + operator is a polymorphic
object.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
29
Object Oriented Programming
Polymorphism:
• For example, calculating the sum of elements of a
list is actually the same for a list of integers, a list of
floats and a list of complex numbers. As long as the
addition operation is defined among the members
of the list, the summation operation would be the
same. If we can implement a polymorphic sum
function, it will be able to calculate the summation
of distinct datatypes, hence it will be polymorphic
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
30
Object Oriented Programming
Polymorphism:
• The ability of a child class to behave and appear
like its parent
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
31
Object Oriented Programming
• The way Python implements OOP is not to the
full extent in terms of the properties.
• Encapsulation is not implemented strongly.
• Inheritance and polymorphism are there.
• Operator overloading is present, that is much
demanded in OOP
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
32
The Class Syntax
• Python is an object oriented programming
language.
• An object is a code structure that includes both
data and actions (methods and special
methods) as one data item.
• Almost everything in Python is an object, with
its properties and methods.
• A Class is like an object constructor, or a
"blueprint" for creating objects.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
33
The Class Syntax
• Python is an object oriented programming
language.
• An object is a code structure that includes both
data and actions (methods and special
methods) as one data item.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
34
The Class Syntax
Class:
Prescription that defines a particular object. The
blueprint of an object.
Class Instance-Object:
A computational structure that has functions and data
fields built according the blueprint, namely the class.
Similar to the construction of buildings according to
an architectural blueprint.
- We can create objects (more than one)
conforming to a class definition. Each of these objects
will have their own data space and in some cases
customized functions.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
35
The Class Syntax
Object: provides the following;
• Methods: Functions that belong to the object.
• Sending a message to an object: Calling a
method of the object.
• Member: Any data or method that is defined in
the class.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
36
The Class Syntax
The "class definition"
• Sending a message to an object: Calling a
method of the object.
• Member: Any data or method that is defined in
the class.
• Create a Class
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
37
The Class Syntax
The "class definition"
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
38
The Class Syntax
.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
39
The Class Syntax
.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
40
The Class Syntax
The "class definition"
1. The name of this class is shape.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
41
The Class Syntax
The "class definition"
1. The name of this class is shape.
2. Any object that will be created according to this
blueprint has three data fields, named color, x
and y.
At the moment of creation, these fields are set
to None (a special value of Python indicating
that there is a variable here but no value is
assigned
yet).
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
42
The Class Syntax
The "class definition"
3. Two member functions, the so-called methods,
are defined: set_color and move_to.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
43
The Class Syntax
The "class definition"
3. Two member functions, the so-called methods,
are defined: set_color and move_to.
- The first takes four arguments, constructs a
tuple of the last three values and stores it into
the color data field of the object.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
44
The Class Syntax
The "class definition"
3. Two member functions, the so-called methods,
are defined: set_color and move_to.
- The second, move_to, takes three arguments
and assign the last two of them to the x and y
data_fields, respectively.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
45
Object Methods
• Objects can also contain methods.
Methods in objects are functions that belong
to the object.
• Let us create a method in the Person class:
Example:
Python Code
Output:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
46
The Class Syntax
The "class definition"
• The dinstinctive keyword self in the blueprint
refers to the particular instance (when an object
is created based on this blueprint).
• The self parameter is a reference to the current
instance of the class, and is used to access
variables that belongs to the class.
• It does not have to be named self,
you can call it whatever you like,
but it has to be the first parameter of any
function in the class:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
47
The Class Syntax
The "class definition"
• The dinstinctive keyword self in the blueprint
refers to the particular instance (when an object
is created based on this blueprint).
• The first argument to all methods (the member
functions) have to be coded as self. That is a
rule. The Python system will fill it out when that
function is activated.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
48
The Class Syntax
The "class definition"
• To refer to any function or any data field of an
object, we use the (.) dot notation. Inside the
class definition, it is self. Outside of the object,
the object is certainly stored somewhere
(a variable or a container).
• The way (syntax) to access the stored object is
followed. Then, this syntax is appended by the
(.) dot which is then followed by the data field
name or the method name.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
49
The Class Syntax
The "class definition"
• Two shape objects and assign them to
two global variables p and s, respectively:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
50
The Class Syntax
The "class definition"
• The object creation is triggered by calling the
class name as if it is a function (i.e. shape()).
• This creates an instance of the class.
Each instance has its private data space.
• Two shape objects are created in the example
and stored in the variables p and s.
• The object stored in p has its private data
space and so does s.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
51
The Class Syntax
The "class definition
• It can be verified by.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
52
The Class Syntax
The "class definition
• When a class is defined, there are a bunch of
methods, which are automatically created, and
they serve the integration of the object
with the Python language.
• For example, what if we issue a print statement
on the object?
• print?
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
53
The Class Syntax
print?
• These default methods can be overwritten
(redefined).
• __str__ is the method that is automatically
activated when a print function has an object to
be printed.
• The built-in print function sends to the object an
__str__ message.
- It means calls the __str__ member function
(method).
• All objects, when created, have a some special
methods predefined.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
54
The Class Syntax
print?
• All objects, when created, have a some special
methods predefined.
• Many of them are out of the scope of this
course.
• __init__ and __str__ are among these special
methods.
• It is possible that the programmer, in the class
definition,
overwrites
(redefines)
these
predefinitions.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
55
The Class Syntax
__str__
• Set to a default definition so that when an
object is printed such an internal location
information is printed:
• We will overwrite this function to output the
color and coordinate information, which will look
like:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
56
The Class Syntax
__init__
• The method that is automatically activated
when the object is first created. It is always
executed when the class is being initiated.
• As default, it will do nothing, but can also be
overwritten.
• It is called automatically every time the class is
being used to create a new object.
• Observe the following statement in the code
above:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
57
The Class Syntax
__init__
• Observe the following statement in the code
above:
• The object creation is triggered by calling the
class name as if it is a function.
• Python (and many other OOP languages) adopt
this syntax for object creation.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
58
The Class Syntax
__init__
• What is done is that the arguments passed to
the class name is sent ‘internally’ to the special
member function __init__.
• We will overwrite it to take two arguments
at object creation, and these arguments will
become the initial values for the x and y
coordinates.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
59
The Class Syntax
__init__
Example:
Python Code
Fall 2023
Output:
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
60
The Class Syntax
__init__
Example:
Python Code
Fall 2023
Output:
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
61
The Class Syntax
The Real Interpreter Example
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
62
The Class Syntax
The Real Interpreter Example
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
63
The Class Syntax
The Real Interpreter Example
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
64
The Class Syntax
The pass Statement
• class definitions cannot be empty, but if you for
some reason have a class definition with no
content, put in the pass statement to avoid
getting an error.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
65
Inheritance
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
66
Inheritance
• To create a class that inherits the functionality
from another class, send the parent class as a
parameter when creating the child class:
• Create a class named Student, which will
inherit the properties and methods from the
Person class:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
67
Inheritance
• To create a class that inherits the functionality
from another class, send the parent class as a
parameter when creating the child class:
• Create a class named Student, which will
inherit the properties and methods from the
Person class:
• Now the Student class has the same properties
and methods as the Person class.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
68
Inheritance
• Use the Student class to create an object,
and then execute the printname method:
• Let us create a method in the Person class:
Example:
Python Code
Output:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
69
Inheritance
• We have created a child class that inherits
the properties and methods from its parent.
• to add the __init__() function to the child
class (instead of the pass keyword).
• Adding
the
__init__()
function
to
the Student class.
Example:
Python Code
Output:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
70
Inheritance
• Adding the __init__() function to the Student
class.
• When you add the __init__() function,
the child class will no longer inherit
the parent's __init__() function.
• The child's __init__() function overrides the
inheritance of the parent's __init__()
function.
• To keep the inheritance of the parent's
__init__() function,
- add a call to the parent's __init__()
function
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
71
Inheritance
• To keep the inheritance of the parent's
__init__() function,
- add a call to the parent's __init__() function
Example:
Python Code
Output:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
72
Inheritance
• the super() Function
- make the child class inherit all the methods
and properties from its parent.
• By using the super() function, you do not
have to use the name of the parent element,
it will automatically inherit the methods and
properties from its parent
Example:
Python Code
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
73
Inheritance
• the super() Function
- make the child class inherit all the methods
and properties from its parent.
Example:
Python Code
Output:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
74
Adding Properties
• Adding a property called graduationyear to
the Student class:
Example:
Python Code
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
75
Adding Properties
• Adding a property called graduationyear to
the Student class:
Example:
Python Code
Output:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
76
Adding Properties
•
In the example, the year 2019 should be a variable,
and passed into the Student class when creating
student objects. To do so, add another parameter in
the __init__() function:
Example:
Python Code
Output:
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
77
Adding Methods
•
•
Adding a method called welcome to the Student
class:
If you add a method in the child class with the same
name as a function in the parent class, the
inheritance of the parent method will be overridden.
Example:
Python Code
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
78
Adding Methods
• Adding a ethod called welcome to the Student class:
Example:
Python Code
OutputHüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
Fall 2023
79
Special Methods
Useful Special Methodson Python’s OOP
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
80
Counter Example
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
81
Important Concepts
Useful Short Notes on Python’s OOP
• It is possible that the derived class overrides the
base class’s member function. But, still want to
access the (former) definition in the base class.
One can access that definition by prefixing the
function call by super(). (no space after the dot).
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
82
Important Concepts
Useful Short Notes on Python’s OOP
• There is no proper destructor in Python. This is because the
Python engine does all the memory allocation and bookkeeping. Though entirely under the control of Python,
sometimes the so-called
• Garbage Collection is carried out. At that moment, all unused
object instances are wiped out of the memory. Before that, a
special member function __del__ is called. If you want to do
a special treatment of an object before it is wiped forever,
you can define the __del__ function. The concept of garbage
collection is complex and it is wrong to assume that __del__
will right away be called even
• if an object instance is deleted by the del statement (del will
mark the object as unused but it will not necessarily trigger a
garbage collection phase).
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
83
Important Concepts
Useful Short Notes on Python’s OOP
• Infix operators have special, associated member
functions (those that start and end with double
underscores). If you want your objects to participate
in infix expressions, then you have to define those.
For example ‘+’ has the associated special member
function __add__. For a complete list and
how-to-do’s, search for “special functions of
Python” and “operator overloading”.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
84
Important Concepts
Useful Short Notes on Python’s OOP
• You can restrict the accessibility of variables defined in
a class. There are three types of accessibility
modifications you can perform: public, private, and
protected. The default is public.
- Public access variables can be accessed anywhere
inside or outside the class.
- Private variables can only be accessed inside the
class definitions. A variable which starts with two
underscores is recognized by programmers as private.
- Protected variables can be accessed within the same
package (file). A variable which starts with a single
underscore is recognized by programmers as protected.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
85
Important Concepts
Useful Short Notes on Python’s OOP
• If a member variable and a member function has
the same name, the member variable override the
member function.
• In Python, you cannot enforce data hiding.
• The users of a class should be careful about using
member variables since they can be deleted or altered
easily.
• To arrange proper deletion of an object, you can define
a “__del__(self)” function èalso called, the destructor.
• The word self can be replaced by any other name.
However, since “self” is the convention and some code
browsers rely on the keyword “self”, it is ideal to use
“self” all the time.
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
86
Important Concepts
• Person, Student, Instructor
– From the workbook:
* Database Recovery.
*
https://pp4eworkbook.github.io/chapters/a_gentle_introd
uction_to_object_oriented_programming/database_r
ecovery.html
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
87
Important Concepts
• In this chapter, you will learn the following
crucial concepts and keywords:
– Object-Oriented Paradigm in Python:
* Encapsulation, inheritance and polymorphism.
* Benefits of the Object-Oriented Paradigm.
* Concepts such as;
- class, instance, object, member, method, message
passing, base class, ancestor, descendant
- base class, ancestor, descendant
Fall 2023
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
88
Questions
Fall 2023
?
Hüseyin Sayın - Ceng240 – Lecture-9 – Object-Oriented Programming Concepts
Download