Home >Backend Development >Python Tutorial >Thoroughly understand Python classes and objects and become a qualified Python developer
In python, a class is a blueprint for creating objects. Classes contain the data structures and behaviors of objects. Objects are instances of classes. The data structures in a class are called properties, and the behaviors in a class are called methods.
Use the class
keyword to define a class. The definition of a class includes the name of the class and the body of the class. The body of a class contains the properties and methods of the class.
For example, the following code defines a class named Person
:
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print("Hello, my name is", self.name)
In this class, the __init__
method is a special function that is called when creating an instance of the class. The __init__
method accepts two parameters: self
(representing an instance of the class) and name
and age
(representing the properties of the object).
greet
The method is a normal method that can be called by instances of the class. The greet
method accepts one parameter: self
(representing an instance of the class).
You can use the class
keyword to create an instance of a class. An instance of a class is a concrete object of the class.
For example, the following code creates two instances of the Person
class:
person1 = Person("John", 25) person2 = Person("Mary", 30)
person1
and person2
are two instances of the Person
class.
You can use the .
operator to access the properties and methods of a class.
For example, the following code accesses the name
attribute of the person1
object:
print(person1.name)
Output:
John
The following code calls the greet
method of the person1
object:
person1.greet()
Output:
Hello, my name is John
Python Supports class inheritance. Class inheritance allows one class to inherit the properties and methods of another class.
For example, the following code defines a class named Student
, which inherits the Person
class:
class Student(Person): def __init__(self, name, age, major): super().__init__(name, age) self.major = major def study(self): print("I am studying", self.major)
In this class, the __init__
method calls super().__init__
to inherit the attributes and methods of the Person
class. study
Method is a normal method that can be called by instances of a class. The study
method accepts one parameter: self
(representing an instance of the class).
Python supports class polymorphism. Class polymorphism allows the same method to have different behaviors for different classes.
For example, the following code defines a function named greet_person
that accepts an instance of the Person
class as a parameter:
def greet_person(person): person.greet()
This function can be called on instances of the Person
class and its derived classes.
For example, the following code calls the greet_person
function to greet person1
and person2
:
greet_person(person1) greet_person(person2)
Output:
Hello, my name is John Hello, my name is Mary
The above is the detailed content of Thoroughly understand Python classes and objects and become a qualified Python developer. For more information, please follow other related articles on the PHP Chinese website!