0% found this document useful (0 votes)
5 views44 pages

Python Object-Oriented Programming Guide

The document provides an overview of Object-Oriented Programming (OOP) concepts such as objects, classes, methods, inheritance, and polymorphism, specifically using Python as the programming language. It discusses the advantages of OOP, the structure of classes and objects in Python, and key features like the __init__ method, method overloading, operator overloading, inheritance, polymorphism, and encapsulation. The instructor, Carlos Alberto Veliz of SENATI, emphasizes the importance of OOP in modern software development.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views44 pages

Python Object-Oriented Programming Guide

The document provides an overview of Object-Oriented Programming (OOP) concepts such as objects, classes, methods, inheritance, and polymorphism, specifically using Python as the programming language. It discusses the advantages of OOP, the structure of classes and objects in Python, and key features like the __init__ method, method overloading, operator overloading, inheritance, polymorphism, and encapsulation. The instructor, Carlos Alberto Veliz of SENATI, emphasizes the importance of OOP in modern software development.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link].

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

Today, OOP is a fundamental programming paradigm for the


development of any type of application, that is why nowadays it is
majority of high-level languages (Like Java, C#, C++ among others)
they support this paradigm and seek to exploit it to the maximum, Python is not the
Exception handling in Python makes working with OOP very easy and enjoyable.
Reason why during this presentation we will analyze in a
detailed the virtues of Python when it comes to software development under
this paradigm.
[Link]
Object-Oriented Programming
❑ Object-oriented programming is a paradigm of
programming that seeks to represent entities or objects by grouping
data and methods that can describe their characteristics and
behavior.
❑ OOP is a programming paradigm that encompasses the concepts of the world
Relevant realities for our problem are modeled through classes.
and objects, and in which our program consists of a series of
interactions between these objects.
[Link]
Advantages of OOP
❑ Promotes reuse and ❑ Prototype construction
code extension. ❑ Streamlines the development of
❑ It allows to create more systems software
complex. ❑ Facilitates teamwork
❑ Relacionar el sistema al ❑ Facilitates the maintenance of
real world. software
❑ Facilitates the creation of
visual programs.
[Link]
Object-Oriented Model
❑To understand OOP, we will review some basic concepts:
1. Objeto 4. Method.
2. Class 5. Interface.
3. Mensaje Inheritance

[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

❑ Python classes do not have constructors or destructors.


explicit. Python classes have something similar to a constructor: the
init method.
[Link]
Python–OOP: The __INIT__ method
❑ __init__ is called immediately after creating an instance of the class.
❑ It would be tempting but incorrect to call this the constructor of the
class. It is tempting because it looks like a constructor (for
convention, __init__ is the first method defined for the class), it acts
as one (is the first piece of code that is executed in a
instance of the newly created class), and it even sounds like one

[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

❑ By convention, this argument is always called self. In the


method __init__, self refers to the newly created object; in others
class methods refer to the instance whose method has been
call.
❑ The __init__ methods can take any number of
arguments, and just like functions, they can be defined with
default values, making them optional for the caller.
[Link]
Python–OOP: The __INIT__ method
❑ By convention, the first argument of any class in Python (the
reference to the instance) is called self.
❑ It fulfills the role of the reserved word this in C++ or Java, but self.
it is not a reserved word in Python, but merely a convention.
❑ Although it needs to specify self explicitly when defining the
method, it is not specified when invoking the method; Python will add it
automatic form.

[Link]
[Link]
Python–OOP: Instantiation of Classes

❑ Creating an object or instantiating a class in Python is very simple.


To instantiate a class, you simply call the class as if
it was a function, passing the arguments defined by the method
__init__. The return value will be the newly created object.

[Link]
Python–OOP: Instantiation of Classes

[Link]
Python–OOP: Delete Objects

❑ Creating new instances is easy, destroying them is even easier. In general,


there is no need to explicitly free the instances, because
they are automatically removed when the variables to which they
assignments leave the scope. Memory losses are rare in
Python.

[Link]
Python - OOP: Data Attributes

❑ Python has data attributes (called instance variables in


Java, and member variables in C++).
❑ To reference this attribute from code that is outside of
the class must grade it with the instance name, [Link],
in the same way I would qualify a function by the name of its
module.

[Link]
Python - OOP: Data Attributes

❑ To refer to data attributes from within the class,


use self as a qualifier. By convention, all attributes of
data is initialized in the __init__ method. However, this is not a
requirement, as attributes, like local variables,
they begin to exist when their first value is assigned.

[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

❑ So if a subclass has an __init__ method,


always overrides the __init__ method of its parent class, even if
this is defined with a list of different arguments. And it is applied to the
same as any other method.

[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

class amphibian(aquatic, terrestrial):


pass
[Link]
Python–OOP: Polymorphism
❑ In object-oriented programming, polymorphism refers to the
the ability of objects of a class to respond to the same
message or event based on the parameters used during its
Invocation. A polymorphic object is an entity that can contain
values of different types during program execution.
(wikipedia)

[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]

You might also like