質問

I am learning Python, and I am trying to understand descriptors better. When I look at this Python online book: http://www.cafepy.com/article/python_attributes_and_methods/ch01s05.html, it says:

  1. If attrname is a special (i.e. Python-provided) attribute for objectname, return it.

I don't understand what Python-provided means. Can someone give me an exemple of such Python-provided attribute that would take precedence over the usual resolution order?

Note: I am only interested in new-style classes (as descriptors don't even apply to old-style as far as I know).

役に立ちましたか?

解決

As you guessed, step 1 is flat-out wrong and doesn't exist.

他のヒント

__class__, for instance:

>>> class Test(object):
    __dict__ = {'__class__' : "dict of Test"}
    def __init__(self):
        self.__dict__['__class__'] = "dict of test"


>>> test = Test()
>>> test.__class__
<class '__main__.Test'>
>>> test.__dict__
{'__class__': 'dict of test'}
>>> Test.__dict__
dict_proxy({'__dict__': {'__class__': 'dict of test'}, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None, '__init__': <function __init__ at 0x02BD2770>})
>>> 

equivalent in old-style-classes:

>>> class Test:
        pass

>>> Test.__dict__["__class__"] = "spam"
>>> test = Test()
>>> test.__class__
<class __main__.Test at 0x02BD1110>
>>> test.__dict__ = {'__class__': "foo"}
>>> test.__class__
<class __main__.Test at 0x02BD1110>

while

>>> test.__dict__ = {'__lolcat__': "bar"}
>>> test.__lolcat__
'bar'

There are many more special attribute names, depending on the type of the object. For instance, functions:

>>> def test():pass

>>> dir(test)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
>>> test.func_closure
>>> test.__dict__['func_closure']='roflcopter'
>>> test.func_closure
>>> test.__dict__['foo']='bar'
>>> test.foo
'bar'

see http://docs.python.org/reference/datamodel.html for an overview

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top