小编max*_*uas的帖子

Python __dict__

该属性__dict__应包含用户定义的属性.但是如果我们打印__dict__一个空类,我也会得到:

__module__
__dict__ 
__weakref__
__doc__
Run Code Online (Sandbox Code Playgroud)

其中Python在__dict__属性中预先填充了类对象类型.

现在,__base__并且__class__也Python中定义的类的对象类型的属性,但不包括在__dict__.

是否有任何规则指定哪个dunder属性包含在一个对象中__dict__哪个不包含?

python magic-methods python-3.x

4
推荐指数
1
解决办法
1055
查看次数

Python __class __()

在Python中,文档__class__被描述为属性.在对象type(元类)中,__class__似乎是一种方法.

如果我们这样做:

>>> class Foo:
        pass

>>> a = Foo()
>>> a.__class__ == type.__class__(a)
True
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是:

  1. 当我们打电话时a.__class__,我们真的在调用这个方法type.__class__(a)吗?
  2. __class__是不是__dict__属性成员的原因a吗?

python types object-model python-3.x python-internals

-1
推荐指数
1
解决办法
2228
查看次数