Classes and Objects
Python is an object-oriented programming language, which means it has built-in support for classes and objects. A class is a blueprint for creating objects. Objects have member variables and have behavior associated with them.
To define a class in Python, you use the class keyword followed by the name of the class. The name of the class should start with a capital letter to follow the PEP 8 style guide.
class MyClass:
pass
In the above example, we have defined a class named MyClass that doesn't have any properties or methods. Now let's add some properties and methods to the class.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def start(self):
print(f"{self.make} {self.model} ({self.year}) started.")
def stop(self):
print(f"{self.make} {self.model} ({self.year}) stopped.")
In the above example, we have defined a class named Car that has three properties: make, model, and year. The __init__ method is a constructor that is called when an object of the class is created. It initializes the properties of the object. The self parameter is a reference to the object itself. It is always the first parameter of any method in a class.
We have also defined two methods: start and stop. These methods define the behavior of the object. The start method starts the car and prints a message to the console, and the stop method stops the car and prints a message to the console.
To create an object of the Car class, we use the following syntax:
my_car = Car("Honda", "Civic", 2022)
In the above example, we have created an object of the Car class and passed in the values for the make, model, and year properties.
We can now call the methods of the my_car object using dot notation:
my_car.start()
my_car.stop()
This will output the following messages to the console:
Honda Civic (2022) started.
Honda Civic (2022) stopped.
In addition to properties and methods, classes in Python can also have class-level variables and methods. Class-level variables are shared across all instances of the class, and class-level methods are methods that are called on the class itself rather than on an instance of the class.
class MyClass:
class_variable = 0
def __init__(self):
MyClass.class_variable += 1
@classmethod
def print_class_variable(cls):
print(f"Class variable value: {cls.class_variable}")
In the above example, we have defined a class-level variable class_variable and a class-level method print_class_variable. The __init__ method increments the \strong> class_variable every time an object of the class is created.
We can call the print_class_variable method on the class itself, rather than on an instance of the class:
MyClass.print_class_variable()
This will output the following message to the console:
Class variable value: 1
Conclusion
Classes in Python are a powerful tool for organizing code and creating reusable code. They allow you to encapsulate data and behavior into a single object and reuse that object throughout your code.
Basic Topics
- Introduction to Python
- Basic Syntax and Data Types
- Variables and Operators
- Input
- Conditional statements
- Loops
- Functions
- List
- Tuples
- Sets
- Dictionary
- Modules
- Packages
- Exception Handling
- Read/Write Files