FAQ

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 ?

George

Search Discussions

  • Mark Dickinson at Oct 2, 2009 at 3:59 pm

    On Oct 2, 3:52?pm, George Sakkis 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
  • Robert Kern at Oct 2, 2009 at 8:47 pm

    George Sakkis 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 believe that ints in the 2.x series still use __cmp__. In Python 3.x, __gt__
    and the rest are defined.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco



    From http Fri Oct 2 22:49:34 2009
    From: http (Paul Rubin)
    Date: 02 Oct 2009 13:49:34 -0700
    Subject: Q: sort's key and cmp parameters
    References: <ha2nmh$2l3$2@reader1.panix.com>
    <ec18cfe1-e122-499e-be5e-0bf1bdbb7c07@t11g2000prh.googlegroups.com>
    <7xvdiyhfdi.fsf@ruckus.brouhaha.com>
    <PNydneyge7tCkFvXnZ2dnUVZ_s2dnZ2d@pdx.net>
    Message-ID: <7xtyyhikrl.fsf@ruckus.brouhaha.com>

    Scott David Daniels <Scott.Daniels at Acm.Org> writes:
    Most cases are moreeasily done with key, and it is
    a good idea to make the most accessible way to a sort be the most
    efficient one. In the rare case that you really want each comparison,
    the cmp-injection function will do nicely (and can be written as a
    recipe.
    I don't think wrapping the sorted objects in an otherwise useless
    special purpose class is "nicely", either from a performance or from a
    code verbosity point of view. I avoid Java and its useless extra
    classes for a reason ;-).
    In short, make the easy path the fast path, and more will use it;
    provide two ways, and the first that springs to mind is the one
    used.
    I think we are saying the same thing. Python 2.x provides two ways
    and you can use whichever one fits the application better. I have
    never understood why Python 3.x finds it necessary to break one of
    them. Maybe I can migrate to Haskell by the time Python 2.x becomes
    deprecated.
  • Terry Reedy at Oct 3, 2009 at 1:51 am

    Robert Kern wrote:
    George Sakkis 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 believe that ints in the 2.x series still use __cmp__. In Python 3.x,
    __gt__ and the rest are defined.
    Correct:
    Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
    (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    5 .__gt__ # notice space after 5
    <method-wrapper '__gt__' of int object at 0x1E1FC058>



    From http Sat Oct 3 06:11:22 2009
    From: http (Paul Rubin)
    Date: 02 Oct 2009 21:11:22 -0700
    Subject: Q: sort's key and cmp parameters
    References: <ha2nmh$2l3$2@reader1.panix.com>
    <7x1vln2bzh.fsf@ruckus.brouhaha.com>
    <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com>
    Message-ID: <7x4oqh5d79.fsf@ruckus.brouhaha.com>

    Raymond Hettinger <python at rcn.com> writes:
    I'm not sure what you mean by this. What are the semantics of
    sorting a tree? Can you outline an example of tree that
    could be sorted easily with a cmp function but not a key function?
    The idea was that you have a list of trees that you want to sort, and
    an ordering relation between trees:

    def gt(tree1, tree2): ...

    where you recursively descend both trees until you find an unequal
    pair of nodes. You're not trying to sort the nodes within a single
    tree.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 2, '09 at 2:52p
activeOct 3, '09 at 1:51a
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase