7. Classes and Objects in Python

Página 36

7. Classes and Objects in Python

Classes and objects are the two main aspects of object-oriented programming. A class is a template for creating objects (a particular instance of a class), and an object is an instance of a class.

7.1 What is a Class?

A class is a prototype for creating objects in Python. It is a logical structure that has some defined attributes and methods. A class is defined using the class keyword.

class MyClass:
  x = 5

In this example, we create a class called MyClass, which has an attribute called x with the value 5.

7.2 What is an Object?

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. Creating an object is done using the constructor function of the class. This method is called when an object is created from a class and allows the class to initialize the object's attributes.

p1 = MyClass()
print(p1.x)

In this example, p1 is an object of class MyClass that has an attribute called x.

7.3 The __init__() function

In Python, the __init__() function is the constructor method that is called when an object is created from a class. This method is useful for doing any initialization you want to do with your object. When you create a __init__() method, you are telling Python that when an object is created from this class, the object must be initialized with the specified values.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

In this example, Person is a class with the __init__() function that takes two arguments, and creates attributes for name and age.

7.4 Object Methods

Objects can also contain methods. Methods on objects are functions that belong to the object.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()

In this example, we define a method in the Person class called myfunc that prints a line of text.

7.5 The self parameter

The self parameter is a reference to the current instance of the class and is used to access variables belonging to the class. It doesn't have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class.

7.6 Modifying Objects

You can modify properties on objects like this:

p1.age = 40

Or you can delete object properties:

del p1.age

You can also delete objects:

del p1

These are the basic concepts of classes and objects in Python. They are fundamental to object-oriented programming in Python and, when combined with other concepts like inheritance and polymorphism, provide a powerful way to structure your code.

Now answer the exercise about the content:

What is a class in Python?

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

You missed! Try again.

Next page of the Free Ebook:

377.1. Classes and Objects in Python: Introduction to Classes and Objects

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