built-in functions as class attributes

Mark Dickinson dickinsm at gmail.com
Mon Dec 8 05:46:12 EST 2008


Here's a curiosity:  after

def my_hex(x):
    return hex(x)

one might expect hex and my_hex to be interchangeable
in most situations.  But (with both Python 2.x and 3.x)
I get:

>>> def my_hex(x): return hex(x)
...
>>> class T(object): f = hex
...
>>> class T2(object): f = my_hex
...
>>> T().f(12345)
'0x3039'
>>> T2().f(12345)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: my_hex() takes exactly 1 argument (2 given)
[36412 refs]

Anyone know what the precise rules that lead to this
behaviour are, or where they're documented?

Surprised'ly yours,

Mark



More information about the Python-list mailing list