6.1. Object-Oriented Programming in Python: Introduction to Object-Oriented Programming

Página 20

6.1. Object Oriented Programming in Python

Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design computer applications and programs. Python, as a high-level programming language, adopts this paradigm and provides all the necessary tools for creating, manipulating, and interacting with objects.

Introduction to Object-Oriented Programming

OOP is based on some main ideas: objects, classes, inheritance, polymorphism and encapsulation. Let's explore each of these ideas in detail.

Objects

In OOP, an object is an entity that contains data and methods for manipulating that data. Data is stored in variables called attributes, while methods are functions that belong to the object. For example, a person object can have attributes such as name, age, height and methods such as walking, talking, sleeping.

Classes

Classes are like building plans for objects. They define the structure of objects, that is, which attributes and methods an object must have. For example, the Person class can define that each person object must have attributes name, age, height and walk, talk, sleep methods.

Inheritance

Inheritance is an OOP feature that allows you to create a new class based on an existing class. The new class inherits all the attributes and methods of the base class, but can also add new ones or modify existing ones. For example, the Employee class can inherit from the Person class and add a new salary attribute and a new work method.

Polymorphism

Polymorphism is the ability of an object to be used as if it were another type. This allows the code to be more flexible and reusable. For example, if the Employee class and the Student class inherit from the Person class, both can be treated as Person objects, even though they have additional attributes and methods.

Encapsulation

Encapsulation is the practice of hiding the internal details of an object and exposing only what is safe and necessary. This protects the object's data and ensures that it is only manipulated through its methods. For example, the age attribute of a person object can be accessed and modified only through the get_age and set_age methods.

Object Oriented Programming in Python

Python supports object-oriented programming with a simple and intuitive syntax. Classes are defined with the class keyword, followed by the class name and an indented block of code. Attributes are defined within the special __init__ method, which is called automatically when a new object is created. Methods are defined like normal functions, but they always take the self argument, which is a reference to the object itself.

Inheritance in Python is performed by passing the base class as an argument to the derived class. Polymorphism is naturally supported since Python is a dynamically typed language. Encapsulation is accomplished through naming conventions, using a single underscore to indicate a private attribute or method.

In summary, object-oriented programming in Python is a powerful tool for creating reusable, modular, and easy-to-maintain code. Learning and mastering OOP in Python is an essential skill for any Python developer.

Now answer the exercise about the content:

What are the main ideas on which Object Oriented Programming (OOP) is based?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

216.2. Object-Oriented Programming in Python: Classes and Objects in Python

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text