Python Object-Oriented Programming Guide
Python Object-Oriented Programming Guide
pe
Python - O.O.
Object-Oriented Programming
[Link]
Specific objective
•Give a general overview of the
object-oriented programming
understanding in a general way the
concepts such as Objects, Classes,
Methods, Inheritance, Polymorphism, etc.
•Conocer e implementar cada uno de los
conceptos claves de la POO usando
Python as a programming language.
[Link]
Carlos Alberto
VELIZ OF VILLA AGUIRRE
Instructor of the School of Information Technologies
Huancayo, Junín
+51 975610017
cveliz@[Link]
[Link]
[Link]
INTRODUCTION
[Link]
1. Object
❑ An object is a unit that encompasses in ❑Example: Car
himself characteristics and ❑Characteristics
necessary behavior for ❑4 wheels
process information. Each object ❑Motor
contains data and functions. And a ❑Gearbox
❑Blue color
the program is built as a
❑Side mirrors
set of objects, or as a single one
object.
[Link]
2. Class
❑ The class is a model or prototype that ❑Example: Vehicle Class
define the variables and methods ❑Number of wheels
common to all objects of a certain ❑Engine type
class. It can also be said that a ❑Number of gearbox speeds
class is a generic template for a of changes
set of similar objects ❑Color
features. ❑Tank capacity
[Link]
3. Message
❑ The message is the way in which objects communicate with each other.
❑ Example:
❑ When we call a function of an object, we will say that we are sending
this message to that object.
4. Method
❑ A Method in OOP is responsible for processing the messages that
they reach an object, a method is nothing more than a function or
procedure belonging to an object.
[Link]
5. Interface
❑ Classes and therefore also objects have parts.
public and private parts. Sometimes we will refer to the part
public interface of an object. It is the only part of the
an object that is visible to the rest of the objects, so that it is
the only thing available to communicate with them.
[Link]
6. Inheritance
❑ Inheritance is one of the most crucial concepts in OOP.
Inheritance basically consists of a class being able to inherit
their variables and methods to several subclasses. This means that
a subclass, apart from its own attributes and methods, has
incorporated the attributes and methods inherited from the
superclass.
[Link]
Python - Classes and Objects
❑ Python is completely object-oriented: you can define your
own classes, inherit from those you define or from the incorporated ones
in the language, and instantiate the classes that I have defined.
❑ In Python, classes are defined using the reserved word class.
followed by the class name, a colon (:) and then,
indented, the body of the class.
[Link]
Python - Classes and Objects
❑ Ejm:
Class Example: #1
pass #2
❑ In this example, the class name is Example and it does not inherit from another.
class. By convention, classes start with a capital letter.
❑ This class does not define attributes but cannot be empty for that.
we use the pass function, equivalent in other languages to using {}
[Link]
Python–OOP: The __INIT__ method
[Link]
Python–OOP: The __INIT__ method
❑ Incorrect, because the object has already been built by the time it
call __init__, and it already has a valid reference to the new instance
from the class. But __init__ is the closest thing to a constructor that will
find in Python, and plays the same role.
❑ The first attribute or variable of each class method, including
__init__ is always a reference to the current instance of the class.
[Link]
Python–OOP: The __INIT__ method
[Link]
[Link]
Python–OOP: Instantiation of Classes
[Link]
Python–OOP: Instantiation of Classes
[Link]
Python–OOP: Delete Objects
[Link]
Python - OOP: Data Attributes
[Link]
Python - OOP: Data Attributes
[Link]
Python–OOP: Data Attributes
[Link]
Python–OOP: Method Overloading
❑ C++ and Java support function overloading by list of
arguments, that is to say a class can have several methods with the
same name, but with arguments in different quantities, or of
different type. Python does not support function overloading. The methods
are defined only by their name and there is a single method per class with
a given name.
[Link]
Python–OOP: Method Overloading
[Link]
Python–OOP: Operator Overloading
❑ Operator overloading allows for the redefinition of certain operators,
like '+' and '-', to use them with the classes we have defined. It
calls operator overloading because we are reusing the
same operator with a number of different uses, and the compiler
decide how to use that operator depending on what it operates on.
[Link]
Python–OOP: Operator Overloading
➢ add ( self, other) ➢ divmod ( self, other)
➢ subtract ( self, other) ➢ pow ( self, other[, modu
➢ mul ( self, other) ➢ and ( self, other)
➢ rmul(self, other) ➢ xor ( self, other)
➢ floordiv ( self, other) ➢ or ( self, other
➢ mod ( self, other)
[Link]
[Link]
Python–OOP: Inheritance
❑ One of the main properties of classes is inheritance. This
property allows us to create new classes from classes
existing, preserving the properties of the original class and
adding other new ones.
❑ The new class obtained is known as a derived class, and the classes to
from which base classes are derived. In addition, each class
derivative can be used as a base class to obtain a new one
derived class.
[Link]
Python - OOP: Inheritance
❑ Definition of a derived class in Python.
class Instrument:
pass
class Guitar(Instrument):
pass
class Bass(Instrument):
pass
[Link]
Python–OOP: Multiple Inheritance
class aquatic:
pass
terrestrial class
pass
[Link]
Python–OOP: Polymorphism
❑ Polymorphism is the process of using an operator or
function of different forms for different input data. In
In practical terms, polymorphism means that if class B inherits
from class A, it does not have to inherit everything about class A, that
it can do some of the things that a different class does
[Link]
Python–OOP: Polymorphism
❑ Unlike Java and C++, polymorphism in Python is not significant.
importance, given its nature as a dynamic language.
❑ In Python, there is no method overloading; the last method that
it is stated that it will replace the previous ones, although it can be obtained.
este comportamiento usando métodos de # argumentos variable
(others, **others)
[Link]
Python–OOP: Encapsulation
❑ To protect the variables from unwanted modifications, one
introduce the concept of encapsulation. The members of a class
They can be divided into public and private. Public members are
those that can be freely accessed from outside the
class. Private members, on the other hand, can only be
accessed by the methods of the class itself
[Link]
Python–OOP: Encapsulation
❑ Encapsulation is achieved in other programming languages.
like Java and C++ using access modifiers that define whether
Anyone can access the method or attribute.
❑ In these languages we have the modifications:
❑ Public -> makes the methods and attributes visible outside the class.
❑ private -> makes the methods and attributes accessible only by
methods within the class.
[Link]
Python–OOP: Encapsulation
❑ In Python, there are no access modifiers.
❑ Access to an attribute or to the methods is determined by its
name: if the name starts with two underscores (and does not end
also with two underscores) it is an attribute or method
private, if not, these are public.
[Link]
[Link]
[Link]
[Link]