- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我以前从未见过其他任何东西像这样工作。
还有其他的东西可以做到这一点吗?
>>> class NothingSpecial:
@classmethod
def meth(cls): pass
>>> NothingSpecial.meth
<bound method classobj.meth of <class __main__.NothingSpecial at 0x02C68C70>>
>>> NothingSpecial.__dict__['meth']
<classmethod object at 0x03F15FD0>
>>> getattr(NothingSpecial, 'meth')
<bound method NothingSpecial.meth of <class '__main__.NothingSpecial'>>
>>> object.__getattribute__(NothingSpecial, 'meth')
<classmethod object at 0x03FAFE90>
>>> type.__getattribute__(NothingSpecial, 'meth')
<bound method NothingSpecial.meth of <class '__main__.NothingSpecial'>>
最佳答案
主要区别在于字典查找不进行额外处理,而属性获取包含额外逻辑(有关所有详细信息,请参阅我的 Descriptor How-To Guide)。
1) 调用 NothingSpecial.__dict__['meth']
使用方括号运算符分派(dispatch)给 dict.__getitem__
进行简单的哈希表查找或引发 < em>KeyError 如果找不到。
2) 调用 NothingSpecial.meth
使用点运算符分派(dispatch)给 type.__getattribute__
进行简单查找,然后是 descriptors 的特例.如果查找失败,则会引发 AttributeError。
In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: __get__(), __set__(), and/or __delete__(). If any of those methods are defined for an object, it is said to be a descriptor.
The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, a.x has a lookup chain starting with a.__dict__['x'], then type(a).__dict__['x'], and continuing through the base classes of type(a) excluding metaclasses.
However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called
希望您发现所有这些对您有所帮助。您正在进行的探索是学习 Python 的好方法:-)
附言您可能还喜欢阅读 Python 2.2 中的原始 Whatsnew entry for descriptors或查看 PEP 252 Guido van Rossum 最初提出这个想法的地方。
关于python - 为什么 cls.__dict__[meth] 与类方法/静态方法的 getattr(cls, meth) 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44596009/
代码先行: class A(object): def foo(self): self.foo = 'foo' def bar(self): self.b
请看下面的片段: class Foo: class_var = "hi" foo = Foo() assert foo.class_var is Foo.class_var assert "c
>>> class A(object): pass ... >>> A.__dict__ >>> A.__dict__.__dict__ Traceback (most recent call la
如果我在 Python 解释器中运行以下代码: >>> object.__dict__ is object.__dict__ False 为什么结果是 False? 最佳答案 object.__dic
我感兴趣的是是否有一种方法可以可靠地内省(introspection) Python 实例以查看它的 __dict__ 尽管程序员可能会在途中抛出任何障碍,因为这将帮助我调试问题,例如意外的引用循环和
属性 __dict__ 应该包含用户定义的属性。但是如果我们打印一个空类的 __dict__,我也会得到: __module__ __dict__ __weakref__ __doc__ Python
我知道函数对象有一个 __dict__ .这很少用于向函数添加属性。有些人甚至用它来为函数添加注释。 我的疑问如下。如果有: def foo(): a = 2 return 在哪里 “一个
我对 dict 进行子类化,以便属性与键相同: class DictWithAttr(dict): def __init__(self, *args, **kwargs): s
我有一个字典,它的大小是 198526。 sys.getsizeof(dic)) # 198526 class Actor(object): def __init__(self):
给定一个任意类 X 作为输入,是否有可能找出 X 的实例是否有一个 __dict__? 我尝试了 hasattr(X, '__dict__'),但这不起作用,因为它检查类对象是否具有 __dict__
我需要定义一个名为“class”的类变量。我想直接在类命名空间中执行此操作,而不是在类方法中。显然,我不能直接说: class MyClass(object): a = 1 b = 2
这个问题在这里已经有了答案: Python class instance __dict__ doesn't contain all instance variables. Why? (3 个答案)
我试图直接使用 X.__dict__['x'] += 1 之类的东西修改类 __dict__ 中的值。那样修改是不可能的,因为类__dict__实际上是一个mappingproxy对象,不允许直接修改
我正在测试预定义的 字典 一个函数的属性,我得到了一个我没想到的结果。考虑以下代码: >>> def func(): x = 7 a = 0 print(fu
为什么这有效: class Bunch(dict): def __init__(self, *args, **kwargs): super(Bunch, self).__ini
代码在前, #Python 2.7 >>>class A(object): pass >>>a1 = A() >>>a2 = A() >>>A.__dict__ dict_proxy({
这个问题在这里已经有了答案: How do you programmatically set an attribute? (5 个答案) 关闭 6 年前。 我被告知我应该避免在我的代码中使用 sel
考虑这个例子,其中一个类 A 的所有实例的 __dict__ 将指向一个全局字典 shared。 shared = {'a': 1, 'b': 2} class A(object): def
有没有理由不这样做来比较两个对象: def __eq__(self, other): return self.__dict__ == other.__dict__ 与检查每个单独的属性相反:
我试图理解 python 中的束模式,我相信可以用以下方式表达: class Bunch: def __init__(self, **kwds): self.__dict__.
我是一名优秀的程序员,十分优秀!