- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
来自 https://stackoverflow.com/a/1529099/156458
To support arbitrary attribute assignment, an object needs a
__dict__
:a dict associated with the object, where arbitrary attributes can bestored. Otherwise, there's nowhere to put new attributes.An instance of
object
does not carry around a__dict__
-- if it did,before the horrible circular dependence problem (since__dict__
, like mosteverything else, inherits fromobject
;-), this would saddle everyobject in Python with a dict, which would mean an overhead of manybytes per object that currently doesn't have or need a dict(essentially, all objects that don't have arbitrarily assignableattributes don't have or need a dict).
...
When the class has the
__slots__
special attribute (a sequence of strings), then theclass
statement (more precisely, the default metaclass,type
) does not equip every instance of that class with a__dict__
(and therefore the ability to have arbitrary attributes), just a finite, rigid set of "slots" (basically places which can each hold one reference to some object) with the given names.
如果一个对象没有__dict__
,它的类必须有一个__slots__
属性吗?
例如,object
的实例没有 __dict__
:
>>> object().__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute '__dict__'
但它也没有__slots__
:
>>> object.__slots__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'object' has no attribute '__slots__'
object
实例有任何属性吗?
有多少种可能性:
__dict__
,它的类有 __dict__
但没有 __slots__
__dict__
,它的类有__slots__
__dict__
,它的类没有__slots__
?是否可以判断一个对象是否具有来自其类的 __dict__
?
如果它的类有 __slots__
,那么它没有 __dict__
,对吗?
如果它的类没有__slots__
,我怎么知道它是否有__dict__
?
最佳答案
对于用户定义的类(在常规 Python 代码中使用 class
关键字定义),类将始终在类上具有 __slots__
,__dict__
在实例上,或两者(如果定义的插槽之一是 '__dict__'
,或者继承链中的用户定义类之一定义了 __slots__
而另一个定义了不是,隐式创建 __dict__
)。因此,这是用户定义类涵盖的四种可能性中的三种。
编辑:更正:从技术上讲,用户定义的类 could have neither ;该类将使用 __slots__
定义,但在定义时间后将其删除(设置类型的机制不需要 __slots__
在类定义完成后继续存在) .任何理智的人都不应该这样做,并且它可能会产生不良的副作用(未经测试的完整行为),但这是可能的。
对于内置类型,至少在 CPython 引用解释器中,它们极不太可能有 __slots__
(如果有,那将是模拟一个用户定义的类,定义它实际上做任何有用的事情)。内置类型通常将其属性存储为 C 级结构上的原始 C 级值和指针,可选择使用显式创建的描述符或访问器方法,这消除了 __slots__
的目的,这只是一个方便的用户定义类的此类结构游戏的有限用途等价物。 __dict__
是选择加入内置类型,默认情况下不启用(尽管选择加入过程相当简单;您需要在某处放置一个 PyObject*
条目结构并在类型定义中为其提供偏移量)。
需要明确的是,__dict__
不必出现在类 中,它也会出现在它的实例 中; __slots__
是类级别的,可以抑制instance上的__dict__
,但对类本身是否有__dict__没有影响
;用户定义的类总是有__dict__
,但如果您小心地适当使用__slots__
,它们的实例就不会。
简而言之:
(Sane) 用户定义的类至少具有 __dict__
(在实例上)或 __slots__
(在类上)之一,并且可以同时具有两者。疯狂的用户定义类可能两者都没有,但只有精神错乱的开发人员才会这样做。
内置类通常两者都没有,可能提供__dict__
,并且几乎从不提供__slots__
,因为这对它们毫无意义。
例子:
# Class has __slots__, instances don't have __dict__
class DictLess:
__slots__ = ()
# Instances have __dict__, class lacks __slots__
class DictOnly:
pass
# Class has __slots__, instances have __dict__ because __slots__ declares it
class SlottedDict:
__slots__ = '__dict__',
# Class has __slots__ without __dict__ slot, instances have it anyway from unslotted parent
class DictFromParent(DictOnly):
__slots__ = ()
# Complete insanity: __slots__ takes effect at class definition time, but can
# be deleted later, without changing the class behavior:
class NoSlotNoDict:
__slots__ = ()
del NoSlotNoDict.__slots__
# Instances have no __dict__, class has no __slots__ but acts like it does
# (the machinery to make it slotted isn't undone by deleting __slots__)
# Please, please don't actually do this
# Built-in type without instance __dict__ or class defined __slots__:
int().__dict__ # Raises AttributeError
int.__slots__ # Also raises AttributeError
# Built-in type that opts in to __dict__ on instances:
import functools
functools.partial(int).__dict__ # Works fine
functools.partial.__slots__ # Raises AttributeError
关于python - 如果一个对象没有 `__dict__` ,它的类必须有一个 `__slots__` 属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46575174/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!