7.2. Classes and Objects in Python: Attributes and Methods of a Class

Página 38

In Python, object-oriented programming is a programming paradigm that allows you to structure programs in a clear, reusable way. In this paradigm, the concepts of classes and objects are fundamental. Let's explore these concepts in detail.

Classes

A class in Python is like a blueprint for creating objects. A class defines a set of attributes that characterize any object that is classified as belonging to the class. Attributes are data members (class and instance variables) and methods, accessed via dot notation.

To define a class in Python, we use the keyword "class" followed by the name of the class. For example:

class Car:
    pass

In this example, we define a class called "Car". The "pass" keyword is used as a placeholder when the code that should go in that location has not yet been written.

Attributes of a class

Attributes of a class are essentially variables that belong to the class. They can be of two types: instance variables and class variables.

Instance variables

Instance variables are unique to each instance of a class. This means that each object in the class has its own copy of the instance variable, and they are not shared between objects. For example:

class Car:
    def __init__(self, make, model):
        self.brand = brand
        self.model = model

In this example, "brand" and "model" are instance variables. Each object of the "Car" class will have its own copies of these variables.

Class variables

Class variables, on the other hand, are shared by all objects in the class. They are defined inside the class, but outside any methods of the class. For example:

class Car:
    number_of_wheels = 4
    def __init__(self, make, model):
        self.brand = brand
        self.model = model

In this example, "number_of_wheels" is a class variable. All objects of the "Car" class will share the same value for this variable.

Methods of a class

Methods of a class are functions that belong to a class. They are used to define behaviors for objects of the class. For example:

class Car:
    number_of_wheels = 4
    def __init__(self, make, model):
        self.brand = brand
        self.model = model
    def accelerate(self):
        print(f'{self.brand} {self.model} is accelerating.')

In this example, "accelerate" is a method of the "Car" class. This method can be called on any object of the "Car" class to make the car accelerate.

Objects

An object is an instance of a class. When a class is defined, only the object's description is defined. Therefore, no memory space is allocated. To allocate memory for an object, we must instantiate the class. For example:

my_car = Car('Ford', 'Mustang')

In this example, "my_car" is an object of class "Car". It has its own values ​​for the "brand" and "model" instance variables, and can use the "accelerate" method.

In short, classes and objects in Python allow for clear, reusable structuring of programs. Classes define attributes and behaviors that characterize objects, while objects are instances of classes.

Now answer the exercise about the content:

What are instance variables and class variables in Python?

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

You missed! Try again.

Next page of the Free Ebook:

397.3. Python Classes and Objects: Encapsulation and Access Modifiers

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