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

IT Specialist Python - Session 5

This document provides an overview of Object-Oriented Programming (OOP) concepts, focusing on classes, objects, and inheritance in Python. It explains the importance of OOP for code reusability and organization, introduces key functions like __init__() and __str__(), and outlines exercises for practical application. Additionally, it covers how to create classes and implement inheritance using Python syntax.

Uploaded by

Alejandra Campos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

IT Specialist Python - Session 5

This document provides an overview of Object-Oriented Programming (OOP) concepts, focusing on classes, objects, and inheritance in Python. It explains the importance of OOP for code reusability and organization, introduces key functions like __init__() and __str__(), and outlines exercises for practical application. Additionally, it covers how to create classes and implement inheritance using Python syntax.

Uploaded by

Alejandra Campos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

IT Specialist - Python

Session 5 – Classes and Objects


What will we see now?
• Object-oriented programming
• Class
• Object
• Builder
• Inheritance
Object-oriented programming

Object Oriented Programming (OOP) is a programming paradigm, that


is, a model or a programming style that gives us some guidelines on
how to work with it. It is based on the concept of classes and objects.

This type of programming is used to structure a software program into


simple, reusable pieces of code blueprints (classes) to create individual
instances of objects.
Object-oriented programming

Why OOP?
Object Oriented Programming allows code to be reusable, organized, and easy to
maintain. Follows the software development principle used by many programmers
DRY (Don’t Repeat Yourself), to avoid duplicating the code and this create efficient
programs.

Additionally, it prevents unwanted access to data or exposure of proprietary code


through encapsulation and abstraction, which we'll talk about in more detail later.
Classes

Why OOP?
A class is a template.

It defines in a generic way how the objects of a certain type are going to be. For
example, a class to represent animals can be called 'animal' and have a series of
attributes, such as 'name' or 'age' (which are normally properties), and a series
with the behaviors that these can have, such as walking or eat, and which in turn
are implemented as methods of the class (functions).
classes in python
Python is an object-oriented programming language.

Almost everything in Python is an object, with it’s properties and


methods.

A class is like an object constructor or a "model" for creating objects.


classes in python
create a class

To create a class, use the keyword class:


Object in Python
create object

Now we can use the class called MyClass to create objects:


Builder
The function __init__()

To understand the meaning of classes, we must understand the


function __init__() incorporated.

All classes have a function called __init__(), which is always executed


when the class starts.

use function __init__() to assign values ​to object properties or other


operations that are required when the object is created
Builder
Create a class called Person, use the function __init__() to assign
values ​for name and age:
Object Description
The function __str__()
The function __str__() controls what should be returned when the
class object is represented as a string.

If the function __str__() is not set, the string representation of the


object is returned
object methods
Objects can also contain methods.
Methods on objects are functions that belong to the object.
parameterself
Parameter self is a reference to the current instance of the class and is used to
access variables that belong to the class.

Doesn't have to be named self, you can call it whatever you want, but it has to be
the first parameter of any function in the class:
Modify or Delete property
Modify property
You can modify properties on objects like this:

Delete property
You can remove properties on objects using the keyword del:

Delete object
You can remove objects using the keyword del:
Exercise
Create a Calculator object that has the qualities:

Properties:
• Owner
• Model
• Record

methods
• Start, you must run a menu to perform basic operations
• Addition
• Subtraction
• Multiplication
• Division
• View Operations History
Python inheritance
Inheritance allows us to define a class that inherits all the methods and properties
of another class.

The Main class is the class from which it is inherited,


also called base class.

The Secondary class is the class that inherits from another class,
also called derived class.
Python inheritance
Create a main class
Any class can be a main class, so the syntax is the same as creating any
other class:

Figure
Geometric
Python inheritance
Create a class called people, with properties: firstname, lastname and a
method printname:
Python inheritance
Create a child class
To create a class that inherits functionality from another class, pass the
parent class as a parameter when creating the child class:

Create a class called Student, which will inherit the properties and
methods of the class people:

Now the Student class has the same properties and methods as the
Person class.
Python inheritance
Add function __init__()

So far we've created a child class that inherits its parent's properties
and methods.

We want to add the __init__() function to the child class (instead of the
keywordpass).
Python inheritance
Use the super() function

Python also has a super() function that will cause the child class to
inherit all of its parent's methods and properties:
Exercise
Create a shape class that derives different geometric figures and that
allows calculating their areas and knowing their sides.
Doubts or Questions

You might also like