[Python-3000] Method descriptors

Christian Heimes lists at cheimes.de
Tue Dec 11 13:49:47 CET 2007


Marcin ‘Qrczak’ Kowalczyk wrote:
> Below is what I have, after some cleaning and renaming. Needs more
> cleaning to plug it into Python modules and conform to Python coding
> standards.

http://bugs.python.org/issue1587
I've added \t to the formatting, changed the name slightly and filled a
bunch of slots.

>>> class Example:
...     id = instancemethod(id)
...     str = instancemethod(str)
...     hash = hash
...
>>> Example.id
<built-in function id>
>>> Example().id
<bound method Example.id of <__main__.Example object at 0x83b3bcc>>

>>> ex = Example()
>>> hex(ex.id())
'0x83b3ea4'
>>> ex.id
<bound method Example.id of <__main__.Example object at 0x83b3ea4>>
>>> ex.str()
'<__main__.Example object at 0x83b3ea4>'

>>> ex.hash()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: hash() takes exactly one argument (0 given)

>>> instancemethod(id)
<instancemethod id at 0x83a15a4>
>>> instancemethod(id).__func__
<built-in function id>

> I'm not convinced that Python on its own needs it. Perhaps Python
> already distinguishes function-like objects from others rigorously
> enough that they should care about being descriptors themselves.
> CFunction does not define descr_get - why? Maybe it should?

I'm sure Guido can answer the question better than me. :)

Christian


More information about the Python-3000 mailing list