why are functions greater than numbers?

D

Dan Stromberg

Why do function objects compare in this way to numbers?
Thanks,
Alan Isaac


True

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
Type "help", "copyright", "credits" or "license" for more information..... return 'abc'
....Traceback (most recent call last):
 
M

MRAB

Why do function objects compare in this way to numbers?
Thanks,
Alan Isaac


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:
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.
 
T

Terry Reedy

Why do function objects compare in this way to numbers?
Thanks,
Alan Isaac


True

In 3.xTraceback (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.
 
E

Emile van Sebille

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top