int rich comparisons

Mark Dickinson dickinsm at gmail.com
Fri Oct 2 17:59:31 CEST 2009


On Oct 2, 3:52 pm, George Sakkis <george.sak... at gmail.com> wrote:
> I stumbled upon the following strangeness (python 2.6.2):
>
> >>> getattr(int, '__gt__')
>
> <method-wrapper '__gt__' of type object at 0x822b7c0>
>
> >>> getattr(5, '__gt__')
>
> Traceback (most recent call last):n
>   File "<stdin>", line 1, in <module>
> AttributeError: 'int' object has no attribute '__gt__'
>
> Is this a bug ?

I don't think so.  Notice that in the first example you're picking
up the __gt__ method of the *type* object, not the int object;  i.e.,
the method that's executed when you do:

>>> int > float
True

You get similar results with __call__:

>>> int.__call__
<method-wrapper '__call__' of type object at 0x135060>
>>> int.__call__(3)
3
>>> (5).__call__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__call__'

Mark



More information about the Python-list mailing list