Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

why are functions greater than numbers?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


alan.isaac at gmail

Jan 24, 2011, 1:51 PM

Post #1 of 7 (177 views)
Permalink
why are functions greater than numbers?

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


benjamin.kaplan at case

Jan 24, 2011, 2:36 PM

Post #2 of 7 (175 views)
Permalink
Re: why are functions greater than numbers? [In reply to]

On Jan 24, 2011 5:31 PM, "Alan" <alan.isaac [at] gmail> wrote:
>
> Why do function objects compare in this way to numbers?
> Thanks,
> Alan Isaac
>
>
> >>> def f(): return
> ...
> >>> f>5
> True
>

Python 2 returned an arbitrary but consistent ordering for almost all
comparisons, just in case you were doing something weird like sorting a list
with mixed types. Python 3 will throw an exception if you try doing
something silly like compare a function to a number.


ian.g.kelly at gmail

Jan 24, 2011, 2:39 PM

Post #3 of 7 (171 views)
Permalink
Re: why are functions greater than numbers? [In reply to]

On Mon, Jan 24, 2011 at 2:51 PM, Alan <alan.isaac [at] gmail> 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/stdtypes.html#comparisons

Python 3 fixes this particular wart.
--
http://mail.python.org/mailman/listinfo/python-list


drsalists at gmail

Jan 24, 2011, 2:40 PM

Post #4 of 7 (172 views)
Permalink
Re: why are functions greater than numbers? [In reply to]

On Mon, Jan 24, 2011 at 1:51 PM, Alan <alan.isaac [at] gmail> 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
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... return 'abc'
...
>>> f > 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: function() > int()
>>>
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Jan 24, 2011, 2:56 PM

Post #5 of 7 (172 views)
Permalink
Re: why are functions greater than numbers? [In reply to]

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

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.
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Jan 24, 2011, 3:13 PM

Post #6 of 7 (171 views)
Permalink
Re: why are functions greater than numbers? [In reply to]

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

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


emile at fenx

Jan 24, 2011, 3:17 PM

Post #7 of 7 (164 views)
Permalink
Re: why are functions greater than numbers? [In reply to]

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
>


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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.