why is self not passed to id()?

castironpi castironpi at gmail.com
Thu Sep 4 16:31:02 EDT 2008


On Sep 4, 3:26 pm, Ruediger <larud... at freenet.de> wrote:
> Hello!
>
> Executing following little program gives me an TypeError.
>
> What makes me wonder is that foo does get an argument passed while bar
> doesn't. Can anyone explain why??????
>
> Thanks
> Ruediger
>
> class foo(list):
>     __hash__ = lambda x: id(x)
>
> class bar(list):
>     __hash__ = id
>
> _s_ = set()
> _s_.add(foo())
> _s_.add(bar())
>
> rue at linux:~> python test01.py
> Traceback (most recent call last):
>   File "test01.py", line 9, in <module>
>     _s_.add(bar())
> TypeError: id() takes exactly one argument (0 given)

The answer is fairly technical.  For member functions to be bound to
instances, they are required to have a __get__ method (which takes
instance and owner as parameters).  'id' does not.

(Why does 'id' not have a __get__ method?)

By contrast,

>>> set.add
<method 'add' of 'set' objects>
>>> dir(_)
['__call__', '__class__', '__delattr__', '__doc__', '__get__',
'__getattribute__
', '__hash__', '__init__', '__name__', '__new__', '__objclass__',
'__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__']

'set.add' does.



More information about the Python-list mailing list