【发布时间】:2017-08-10 00:03:00
【问题描述】:
一个非常基本的问题。
我有一些虚拟课程:
class SomeClass:
""" just a docstring"""
a = 5
dir(SomeClass),返回与该类关联的以下属性:
>>> dir(SomeClass)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__form
at__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_s
ubclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclas
shook__', '__weakref__', 'a']
>>>
我注意到,如果我调用 SomeClass.__getattribute__,即使我向它传递了一个参数(它说它需要),错误消息说我没有传递任何参数。
>>> SomeClass.__getattribute__('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected 1 arguments, got 0
>>>
注意 - 并不是我想要以这种方式调用 __getattribute__ 的用例。 (实际上这样做似乎不正确,我想在实例上调用它取而代之的是 SomeClass ,它确实有效)。 我只是对 __getattribute__ 的行为感到好奇:为什么错误消息说它有 0 个参数,而我实际上是在向它传递一个参数?我并不否认我的用法不正确 -只是,为什么 __getattribute__ 不承认它被传递了一个参数?
我确实注意到 __getattribute__ 被列为“slotwrapper”。这有关系吗?我很难弄清楚调用 slotwrapper 和调用函数之间的真正区别是什么。这个 slotwrapper 是否因为我调用它的方式而抛弃了我传递的参数?
>>> SomeClass.__getattribute__
<slot wrapper '__getattribute__' of 'object' objects>
>>>
【问题讨论】:
标签: python-3.x function getattribute