简体   繁体   English

类和数据属性之间有什么区别?

[英]What is the difference between class and data attributes?

To quote diveintopython, 引用diveintopython,

"You already know about data attributes, which are variables owned by a specific instance of a class. Python also supports class attributes, which are variables owned by the class itself." “您已经知道数据属性,它们是类的特定实例所拥有的变量。Python还支持类属性,它们是由类本身拥有的变量。”

In what sense are class attributes owned by a class? 从什么意义上讲,一个类拥有类属性? If you change the value of a class attribute in a specific instance, that change is only reflected in that instance (and not in other instances of the class). 如果您在特定实例中更改类属性的值,则该更改仅反映在该实例中(而不在类的其他实例中)。

From my vantage point this makes class attributes fundamentally the same as data (ie instance) attributes (notwithstanding the syntactic differences). 从我的角度来看,这使类属性与数据(即实例)属性基本相同(尽管在语法上有所不同)。

In C++ change the value of a "class variable", and that change is reflected in all instances. 在C ++中,更改“类变量”的值,并且该更改将反映在所有实例中。

What is the difference between the two? 两者有什么区别?

I think that this example will explain the meaning to you. 我认为该示例将向您解释其含义。

class A(object):
    bar = 1

a = A()
b = A()
b.bar = 2
print a.bar  # outputs 1
A.bar = 3
print a.bar  # outputs 3
print b.bar  # outputs 2

In this case b.bar will be owned by instance after b.bar = 2 but a.bar will still be owned by class. 在这种情况下, b.bar将在b.bar = 2之后由实例拥有,但a.bar仍将由类拥有。 That is why it will be changed on instance after changing it on class and b.bar will not. 这就是为什么在类上对其进行更改后它将在实例上进行更改,而b.bar将不会进行更改的原因。

This question is a duplicate of this one : 这个问题是一个重复这一个

>>> class B(object):
...     foo = 1
... 
>>> b = B()
>>> b.__dict__
{}
>>> b.foo = 2
>>> b.__dict__
{'foo': 2}

When you assign a value to b , you add an instance variable; 当给b赋值时,添加了一个实例变量。 you're not modifying the class attribute. 您没有修改class属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类和实例属性有什么区别? - What is the difference between class and instance attributes? Python class方法之间如何共享变量,实例属性和class属性有什么区别 - Python how to share variable between class methods, and what is the difference between instance attributes and class attributes 实例属性和类属性之间的区别 - Difference between instance attributes and class attributes Python中的类属性,实例属性和实例方法之间的区别 - Difference between Class Attributes, Instance Attributes, and Instance Methods in Python 我了解Python中的实例属性和类属性之间的区别吗? - do I understand the difference between instance attributes and class attributes in Python? 为子类和父类中的属性赋值的区别 - Difference between assigning values to attributes in child class and parent class __init__() 内部和外部变量之间的区别(类和实例属性) - difference between variables inside and outside of __init__() (class and instance attributes) 对象的属性集与其命名空间之间有什么区别? - What is the difference between an object's set of attributes and its namespace? SimpleNamespace 和空类定义有什么区别? - What is the difference between SimpleNamespace and empty class definition? 子类中的赋值和覆盖有什么区别? - What is the difference between assignment and overriding in a child class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM