Velocity Reviews > why are functions greater than numbers?

# why are functions greater than numbers?

Alan
Guest
Posts: n/a

 01-24-2011
Why do function objects compare in this way to numbers?
Thanks,
Alan Isaac

>>> def f(): return

....
>>> f>5

True

Ian Kelly
Guest
Posts: n/a

 01-24-2011
On Mon, Jan 24, 2011 at 2:51 PM, Alan <(E-Mail Removed)> wrote:
> Why do function objects compare in this way to numbers?
> Thanks,
> Alan Isaac
>
>
>>>> def f(): return

> ...
>>>> f>5

> True

http://docs.python.org/library/stdty...ml#comparisons

Python 3 fixes this particular wart.

Dan Stromberg
Guest
Posts: n/a

 01-24-2011
On Mon, Jan 24, 2011 at 1:51 PM, Alan <(E-Mail Removed)> wrote:
> Why do function objects compare in this way to numbers?
> Thanks,
> Alan Isaac
>
>
>>>> def f(): return

> ...
>>>> f>5

> True
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

They shouldn't, but did in 2.x, and no longer do in 3.x:

\$ /usr/local/cpython-3.1/bin/python3
cmd started 2011 Mon Jan 24 02:39:50 PM
Python 3.1.2 (r312:79147, Aug 18 2010, 18:21:44)
[GCC 4.4.3] on linux2
>>> def f():

.... return 'abc'
....
>>> f > 5

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: function() > int()
>>>

MRAB
Guest
Posts: n/a

 01-24-2011
On 24/01/2011 21:51, Alan wrote:
> Why do function objects compare in this way to numbers?
> Thanks,
> Alan Isaac
>
>
>>>> def f(): return

> ...
>>>> f>5

> True
>

In Python 2 any object can be compared in this way to any other. The
result is arbitrary but consistent.

In Python 3 that has changed because in practice it's more trouble than
it's worth:

>>> def f(): return

>>> f>5

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
f>5
TypeError: unorderable types: function() > int()

It's usually a good sign that there's a bug somewhere.

Terry Reedy
Guest
Posts: n/a

 01-24-2011
On 1/24/2011 4:51 PM, Alan wrote:
> Why do function objects compare in this way to numbers?
> Thanks,
> Alan Isaac
>
>
>>>> def f(): return

> ...
>>>> f>5

> True

In 3.x
>>> def f(): pass

>>> f > 5

Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
f > 5
TypeError: unorderable types: function() > int()

There is a historical explanation in many past posts and probably in the
FAQ.

--
Terry Jan Reedy

Emile van Sebille
Guest
Posts: n/a

 01-24-2011
On 1/24/2011 1:51 PM Alan said...
> Why do function objects compare in this way to numbers?

To provide ordering capabilities. IIRC, comparisons of differing types
are arbitrary but consistent.

Emile

> Thanks,
> Alan Isaac
>
>
>>>> def f(): return

> ...
>>>> f>5

> True
>