why are functions greater than numbers?

Discussion in 'Python' started by Alan, Jan 24, 2011.

  1. Alan

    Alan Guest

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


    >>> def f(): return

    ....
    >>> f>5

    True
     
    Alan, Jan 24, 2011
    #1
    1. Advertising

  2. Alan

    Ian Kelly Guest

    Ian Kelly, Jan 24, 2011
    #2
    1. Advertising

  3. On Mon, Jan 24, 2011 at 1:51 PM, Alan <> 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()
    >>>
     
    Dan Stromberg, Jan 24, 2011
    #3
  4. Alan

    MRAB Guest

    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.
     
    MRAB, Jan 24, 2011
    #4
  5. Alan

    Terry Reedy Guest

    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
     
    Terry Reedy, Jan 24, 2011
    #5
  6. 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
    >
     
    Emile van Sebille, Jan 24, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BobRoyAce
    Replies:
    7
    Views:
    475
    Peter Blum
    Mar 3, 2005
  2. falcon
    Replies:
    10
    Views:
    19,227
    Roedy Green
    Feb 24, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,226
    Smokey Grindel
    Dec 2, 2006
  4. Dwight Army of Champions
    Replies:
    4
    Views:
    2,896
    John H.
    Mar 17, 2010
  5. Replies:
    1
    Views:
    726
Loading...

Share This Page