gpt4 book ai didi

Strange behavior when python class variables and instance variables overlap(当python类变量和实例变量重叠时会出现奇怪的行为)

转载 作者:bug小助手 更新时间:2023-10-28 22:03:40 25 4
gpt4 key购买 nike



class TestClass():
class_v = 0

def __init__(self) -> None:
self.class_v += 1
TestClass.class_v += 1



print(TestClass.class_v) # 0

class_a = TestClass()
class_b = TestClass()

print(f'class_a.class_v = {class_a.class_v}') # 1
print(f'class_b.class_v = {class_b.class_v}') # 2
print(TestClass.class_v) # 2

In the python code above, when the names of the class variable and instance variable are the same, two instances were created with the same class, but the values are different when the variable is imported. If it is an instance variable, both must be 1, and if it is a class variable, both must be 2. How does python work?

在上面的Python代码中,当类变量和实例变量的名称相同时,使用相同的类创建了两个实例,但在导入变量时值不同。如果它是实例变量,则两者都必须为1;如果它是类变量,则两者都必须为2。


# ex
TestClass.class_v += 1
self.class_v += 1

If you change the order of the code blocks above, 2 and 3 are output instead of 1 and 2.

如果更改上述代码块的顺序,则会输出2和3,而不是1和2。


更多回答
优秀答案推荐

It may help to think of them as "attributes", not "variables". I'm guessing your confusion comes from thinking that the class's class_v and the instance's class_v are the same variable. They are not.

将它们视为“属性”而不是“变量”可能会有所帮助。我猜您的困惑来自于认为类的类_v和实例的类_v是同一个变量。他们不是。


The class and the instance are two different objects, each with their own attributes, but instances can inherit attributes they haven't been assigned from their class. In self.class_v += 1, self doesn't have a class_v yet, so it retrieves a value from TestClass.class_v, then assigns the new value to self.class_v. In TestClass.class_v += 1, TestClass already has its own class_v, so the retrieval and assignment both deal with the same attribute.

类和实例是两个不同的对象,每个对象都有自己的属性,但实例可以继承它们没有从类中分配的属性。在self.class_v+=1中,self还没有class_v,所以它从TestClass.class_v中检索一个值,然后将新值赋给self.class_v。在TestClass.class_v+=1中,TestClass已经有了自己的class_v,所以检索和赋值都处理相同的属性。




How does python work?



According to Random Remarks

根据随机评论



If the same attribute name occurs in both an instance and in a class,
then attribute lookup prioritizes the instance



So python first look for instance variable, if it does not exist then class attribute is used.

因此,Python首先查找实例变量,如果实例变量不存在,则使用类属性。


更多回答

To simplify things, we can say that self.class_v += 1 is equivalent to self.class_v = self.class_v + 1, the right part of which fallbacks to TestClass.class_v whereas the left part sets an attribute in the instance's .__dict__.

为了简单起见,我们可以说self.class_v+=1等同于self.class_v=self.class_v+1,它的右侧部分回落到TestClass.class_v,而左侧部分在实例的.__dict__中设置一个属性。

@luther Does doing self.class_v += 1 mean that self can refer to class variables, not instance variables? If then, Is this the right order to find the variables self.class_v += 1 below? 1. Locate the instance variable for class_v. 2. If there is no 1, find the class variable for class_v. 3. If no 2, create a class_v instance variable.

@Luther执行self.class_v+=1是否意味着self可以引用类变量,而不是实例变量?如果是这样,这是否是查找变量self.class_v+=1的正确顺序?1.找到CLASS_V.的实例变量。2.如果没有1,则找到CLASS_V.3的CLASS变量。如果没有2,则创建CLASS_V实例变量。

Yes, the retrieval of an attribute can inherit from the class, while assignment will assign directly into the object. A += statement does both retrieval and assignment, which may be what lead to your confusion.

是的,属性的检索可以从类继承,而赋值将直接赋值到对象中。+=语句既执行检索又执行赋值,这可能会导致您的困惑。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com