Classes and Objects

Python Classes and Objects

Python is a powerful object-oriented programming language, where almost everything is an object with its own properties and methods. Understanding how to create and use classes and objects is crucial for harnessing the full potential of Python. Essentially, a class serves as a blueprint for creating objects, encapsulating data, and defining behaviors.

Creating a Class

To create a class in Python, the class keyword is used. A class can have properties (attributes) and methods (functions).

class Book: genre = "Fiction" print(Book.genre) # Expected output: Fiction 

Instantiating Objects

Once you have a class, you can create objects from it. Each object is an instance of that class.

my_book = Book() print(my_book.genre) # Expected output: Fiction 

The __init__() Method

The __init__() method is a special method in Python classes. It is automatically called when a new object is created from a class. It’s commonly used to initialize the object’s properties.

class Book: def __init__(self, title, pages): self.title = title self.pages = pages my_book = Book("1984", 328) print(my_book.title) # Expected output: 1984 print(my_book.pages) # Expected output: 328 

The __str__() Method

To control what is returned when an object is represented as a string, you can define the __str__() method. This can be particularly useful for debugging and logging.

class Book: def __init__(self, title, pages): self.title = title self.pages = pages def __str__(self): return f"'{self.title}' has {self.pages} pages." my_book = Book("1984", 328) print(my_book) # Expected output: '1984' has 328 pages. 

Object Methods

Methods are functions that belong to the object. They allow the object to perform actions and can manipulate the object’s attributes.

class Book: def __init__(self, title, pages): self.title = title self.pages = pages def summary(self): print(f"'{self.title}' is {self.pages} pages long.") my_book = Book("1984", 328) my_book.summary() # Expected output: '1984' is 328 pages long. 

The self Parameter

The self parameter is a reference to the current instance of the class and is used to access variables that belong to the class. While it’s conventionally named self, you can name it anything you like. It must be the first parameter of any function in the class.

class Book: def __init__(this_instance, title, pages): this_instance.title = title this_instance.pages = pages def summary(this_instance): print(f"'{this_instance.title}' is {this_instance.pages} pages long.") my_book = Book("1984", 328) my_book.summary() # Expected output: '1984' is 328 pages long. 

Modifying Object Properties

You can modify the attributes of an object after it has been created.

my_book.pages = 350 print(my_book.pages) # Expected output: 350 

Deleting Object Properties

You can remove an attribute from an object using the del keyword.

del my_book.pages # print(my_book.pages) # This will raise an AttributeError because 'pages' has been deleted. 

Deleting Objects

Similarly, you can delete the entire object using the del keyword.

del my_book # print(my_book) # This will raise a NameError because 'my_book' has been deleted. 

The pass Statement

If you need to create a class without any content, use the pass statement to avoid errors. This can be helpful as a placeholder while planning your class structure.

class Book: pass # Example of creating an object from an empty class empty_book = Book() print(empty_book) # Expected output: <__main__.Book object at 0x...> 

Exercise

When representing a class object as a string, which method determines what is returned?

  • __init__()
  • __str__()
  • __return__()

Leave a Comment

Importance of a balanced diet in how to lose weight.