对象与类变量

发布于 2024-09-27 04:11:53 字数 276 浏览 0 评论 0 原文

这是一个完全理论上的问题。假设有以下代码:

>>> class C:
...     a = 10
...     def f(self): self.a = 999
...
>>>
>>> C.a
10
>>> c = C()
>>> c.a
10
>>> c.f()
>>> c.a
999

此时,类变量Ca 是否仍然可以通过对象c 访问?

This is a completely theoretical question. Suppose the following code:

>>> class C:
...     a = 10
...     def f(self): self.a = 999
...
>>>
>>> C.a
10
>>> c = C()
>>> c.a
10
>>> c.f()
>>> c.a
999

At this point, is class variable C.a still accessible through the object c?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弄潮 2024-10-04 04:11:53

是的,尽管 c.__class__.atype(c).a。两者略有不同,旧式类(希望这些现在都已经死了 - 但你永远不知道......)的 type() (并且 __class__ 按预期工作),而对于新式类,type()__class__ 相同,除非对象覆盖属性访问。

Yes, though c.__class__.a or type(c).a. The two differ slightly in that old-style classes (hopefully, those are all dead by now - but you never know...) have a type() of <type 'instance'> (and __class__ works as expected) while for new-style classes, type() is identical to __class__ except when the object overrides attribute access.

傲性难收 2024-10-04 04:11:53

在类实例上分配给它后,就会有一个名为 a 的类属性和一个名为 a 的实例属性。我举例说明:

>>> class Foo(object):
...     a = 10
... 
>>> c = Foo()
>>> c.a
10
>>> c.a = 100  # this doesn't have to be done in a method
>>> c.a   # a is now an instance attribute
100
>>> Foo.a  # that is shadowing the class attribute
10
>>> del c.a  # get rid of the instance attribute
>>> c.a     # and you can see the class attribute again
10
>>> 

区别在于,一个作为 Foo.__dict__ 中的条目存在,另一个作为 c.__dict__ 中的条目存在。当您访问 instance.attribute 时,如果存在,则返回 instance.__dict__['attribute'],如果不存在,则返回 type(instance).__dict__['attribute '] 已检查。然后检查该类的超类,但这会变得稍微复杂一些。

但无论如何,要点是不一定非得是其中之一。类和实例都可以具有具有相同名称的不同属性,因为它们存储在两个单独的字典中。

After you assign to it on the class instance, there is both a class attribute named a and an instance attribute named a. I illustrate:

>>> class Foo(object):
...     a = 10
... 
>>> c = Foo()
>>> c.a
10
>>> c.a = 100  # this doesn't have to be done in a method
>>> c.a   # a is now an instance attribute
100
>>> Foo.a  # that is shadowing the class attribute
10
>>> del c.a  # get rid of the instance attribute
>>> c.a     # and you can see the class attribute again
10
>>> 

The difference is that one exists as an entry in Foo.__dict__ and the other exists as an entry in c.__dict__. When you access instance.attribute, instance.__dict__['attribute'] is returned if it exists and if not then type(instance).__dict__['attribute'] is checked. Then the superclasses of the class are checked but that gets slightly more complicated.

But at any rate, the main point is that it doesn't have to be one or the other. A class and an instance can both have distinct attributes with identical names because they are stored in two separate dicts.

无戏配角 2024-10-04 04:11:53

所有类变量都可以通过从该类实例化的对象来访问。

>>> class C:
...     a = 10
...     def f(self): self.a = 999
... 
>>> C.a
10
>>> c = C()
>>> c.a
10
>>> c.f()
>>> c.a
999
>>> c.__class__.a
10
>>> c.a
999
>>> del(c.a) 
>>> c.a
10

首先在对象命名空间中搜索属性,然后在类中搜索。

All class variables are accessible through objects instantiated from that class.

>>> class C:
...     a = 10
...     def f(self): self.a = 999
... 
>>> C.a
10
>>> c = C()
>>> c.a
10
>>> c.f()
>>> c.a
999
>>> c.__class__.a
10
>>> c.a
999
>>> del(c.a) 
>>> c.a
10

Attributes are first searched within the object namespace and then class.

为人所爱 2024-10-04 04:11:53

是的,您可以从对象 c 访问 a,就像 ca 一样。该值最初为 10。

但是,如果您调用 cf()ca 的值现在将为 999,但是 Ca 仍将是 10。同样,如果您现在将 Ca 更改为 1000,ca 仍将是 999。

基本上,当您实例化一个实例时对于 C,它将使用类变量作为自己的 a 值,直到您更改该实例的 a 值code>,在这种情况下,它将不再与类“共享”a

Yes, you can access a from an object c, à la c.a. The value would initially be 10.

However, if you call c.f(), the value of c.a will now be 999, but C.a will still be 10. Likewise, if you now change C.a to, say, 1000, c.a will still be 999.

Basically, when you instantiate an instance of C, it will use the class variable as its own a value, until you change the value of that instance's a, in which case it will no longer "share" a with the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文