Python makes it easy: they are all initial assignments, what is the difference between class parameters and real column attributes?

In object-oriented programming, Python provides powerful features to define classes and create objects with attributes and behaviors. When using classes, master two important concepts: class parameters and instance attributes. In this blog post, we'll explore the difference between these two concepts, their use cases, and their impact on the behavior and flexibility of your Python code.

Class parameters:

Class parameters, also known as class attributes, are variables that are shared among all instances of a class. They hold data common to all objects of that class. Here are some key points to consider:
Declaration: Class parameters are usually defined directly inside the class definition, outside of any methods.
Shared data: Since class parameters are shared, any modifications made to them are reflected in all instances of the class.
Initialization: Class parameters are usually given default values ​​which are used by all instances, but they can also be individually modified if desired.
Access: Class parameters can be accessed using the class name itself followed by the parameter name, no instance required.

class Car:
    wheels = 4
    doors = 4
    engine = "V6"

car1 = Car()
print(car1.wheels)  # 输出:4

car2 = Car()
print(car2.doors)   # 输出:4

Instance attributes:

Instance attributes are attributes unique to each instance of a class. They represent unique data that can vary between different objects. Consider the following aspect of instance attributes:
Declaration: Instance attributes are defined within the class's init() method, which serves as the class's constructor. Each instance can have its own set of instance attributes.
Individual data: Unlike class parameters, instance attributes are not shared between instances. Each instance maintains its own collection of property values.
Initialization: Instance properties are typically initialized with parameters passed to the init() method during object creation. They allow customizing property values ​​on a per-instance basis.
Access: Access instance attributes using the instance name followed by the attribute name. Each instance can independently access and modify its own instance attributes.

class Car:
    def __init__(self, wheels, doors, engine):
        self.wheels = wheels
        self.doors = doors
        self.engine = engine

car1 = Car(4, 4, "V6")
print(car1.wheels)  # 输出:4
print(car1.doors)   # 输出:4
print(car1.engine)  # 输出:V6

car2 = Car(2, 2, "Electric")
print(car2.wheels)  # 输出:2
print(car2.doors)   # 输出:2
print(car2.engine)  # 输出:Electric

Summarize:

Knowing when to use class parameters or instance attributes is critical to designing efficient and flexible classes. Here are some guidelines:

类参数: Use class parameters when you want to store data that is shared between all instances of the class. For example, constants, default values, or settings that are consistent across objects. Class parameters provide an easy way to define and access shared data.

实例属性: Use instance attributes when you need to store per-instance specific data. They allow customization and variation of property values ​​between objects. Instance properties are especially useful when you want each object to be able to maintain its state independently.

English original

click

Guess you like

Origin blog.csdn.net/robot_learner/article/details/130880469