Python object-oriented programming classes are a way of creating and organizing code that allows you to define your own types of objects. Objects are collections of data and behavior that represent some entity or concept in your program. For example, you can create a class called Person that has attributes like name, age, and address and methods like walk, talk, and greet.
To create a class in Python, you use the keyword class followed by the name of the class and a colon. Then, you write the body of the class, which can include special methods like init() and str() that define how the class is initialized and represented, as well as other methods that define the behavior of the class. For example, this is how you can define a simple Person class:
class Person:
def init(self, name, age):
self.name = name
self.age = age
def str(self):
return f"{self.name} ({self.age})"
def greet(self):
print(f"Hello, my name is {self.name}.")
To create an object or an instance of a class, you call the class name with parentheses and pass any arguments that the init() method requires. For example, this is how you can create a Person object named john:
john = Person("John", 36)
You can access the attributes and methods of an object using the dot (.) operator. For example, this is how you can print the name and age of john and call the greet() method:
print(john.name) # Prints John
print(john.age) # Prints 36
john.greet() # Prints Hello, my name is John.
Python also supports inheritance, which is a way of creating new classes from existing ones by reusing and extending their functionality. You can create a subclass or a child class by specifying the parent class or the base class in parentheses after the subclass name. For example, this is how you can create a Student class that inherits from the Person class:
class Student(Person):
def init(self, name, age, school):
super().init(name, age) # Call the parent class init() method
self.school = school # Add a new attribute
def study(self):
print(f"I am studying at {self.school}.")
You can create a Student object by passing the name, age, and school arguments to the Student class. For example, this is how you can create a Student object named alice:
alice = Student("Alice", 18, "MIT")
You can access the attributes and methods of alice as well as those inherited from the Person class. For example, this is how you can print the name, age, and school of alice and call the greet() and study() methods:
print(alice.name) # Prints Alice
print(alice.age) # Prints 18
print(alice.school) # Prints MIT
alice.greet() # Prints Hello, my name is Alice.
alice.study() # Prints I am studying at MIT.
Here are some additional examples of how OOP classes can be used in Python:
- Create a class to represent a bank account. This class would have attributes such as account number, balance, and account type. It would also have behaviors such as deposit(), withdraw(), and transfer().
- Create a class to represent a car. This class would have attributes such as make, model, year, and mileage. It would also have behaviors such as drive(), accelerate(), and brake().
- Create a class to represent a student. This class would have attributes such as name, student ID, and GPA. It would also have behaviors such as enroll(), drop(), and take_exam().
Python object-oriented programming classes are a powerful tool for modeling real-world entities and concepts in your program. They allow you to encapsulate data and behavior into reusable and modular units of code. They also enable you to create hierarchies of classes that share common features and specialize in different ways.