python属性和变量_python中实例变量和属性之间的差异?

我做了研究,是的,实例变量和属性是不同的,这是因为类实例和类有单独的字典。正如documentation中所说:A class instance is created by calling

a class object (see above). A class

instance has a namespace implemented

as a dictionary which is the first

place in which attribute references

are searched. When an attribute is not

found there, and the instance’s class

has an attribute by that name, the

search continues with the class

attributes.

所以基本上Noddy结构包含实例变量。Noddy_成员持有属性。此外:Attribute assignments and deletions

update the instance’s dictionary,

never a class’s dictionary. If the

class has a setattr() or

delattr() method, this is called instead of updating the instance

dictionary directly.

同时:If a class attribute is found that is

a user-defined function object or an

unbound user-defined method object

whose associated class is the class

(call it C) of the instance for which

the attribute reference was initiated

or one of its bases, it is transformed

into a bound user-defined method

object whose im_class attribute is C

and whose im_self attribute is the

instance. Static method and class

method objects are also transformed,

as if they had been retrieved from

class C; see above under “Classes”.

See section Implementing Descriptors

for another way in which attributes of

a class retrieved via its instances

may differ from the objects actually

stored in the class’s dict. If no

class attribute is found, and the

object’s class has a getattr()

method, that is called to satisfy the

lookup.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值