operator overloading on built-ins

Discussion in 'Python' started by r.grimm@science-computing.de, Nov 9, 2007.

  1. Guest

    Hallo,
    could you explaint me the difference between the two following
    statements.


    Python 2.5 (r25:51908, Oct 7 2006, 23:45:05)
    [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> (1).__cmp__(10)

    -1
    >>> 1.__cmp__(10)

    File "<stdin>", line 1
    1.__cmp__(10)
    ^
    SyntaxError: invalid syntax

    The first works as expect, but the second.

    Regards Rainer
     
    , Nov 9, 2007
    #1
    1. Advertising

  2. wrote:
    >>>> (1).__cmp__(10)

    > -1


    Integer object "(1)" followed by method call ".__cmp__(10)"

    >>>> 1.__cmp__(10)

    > File "<stdin>", line 1
    > 1.__cmp__(10)
    > ^
    > SyntaxError: invalid syntax


    Floating point number "1." followed by "__cmp__(10)".

    STeVe
     
    Steven Bethard, Nov 9, 2007
    #2
    1. Advertising

  3. On Thu, 08 Nov 2007 22:53:16 -0800, r.grimm wrote:

    >>>> (1).__cmp__(10)

    > -1


    As the dot is an operator like ``+`` or ``/`` you can also add spaces to
    avoid the ambiguity:

    In [493]: 1 . __cmp__(10)
    Out[493]: -1

    In [494]: 1 .__cmp__(10)
    Out[494]: -1

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Nov 9, 2007
    #3
    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. dan

    overriding built-ins

    dan, Sep 28, 2003, in forum: Python
    Replies:
    7
    Views:
    422
    Andrew Dalke
    Oct 2, 2003
  2. Pettersen, Bjorn S

    RE: overriding built-ins

    Pettersen, Bjorn S, Oct 2, 2003, in forum: Python
    Replies:
    1
    Views:
    412
    John Roth
    Oct 2, 2003
  3. Marco Aschwanden
    Replies:
    3
    Views:
    300
    Carlos Ribeiro
    Sep 3, 2004
  4. Carlos Ribeiro
    Replies:
    3
    Views:
    295
  5. Jp Calderone
    Replies:
    3
    Views:
    286
    Carlos Ribeiro
    Sep 6, 2004
Loading...

Share This Page