What's more pythonic?

Discussion in 'Python' started by Dan Stromberg, Sep 13, 2006.

  1. Is sample1 or sample2 the more pythonic way of comparing?

    class sample1
    def __init__(self, a, b):
    self.a = a
    self.b = b
    def __cmp__(self, other):
    return self.a.__cmp__(other.a)

    ....or:

    class sample2
    def __init__(self, a, b):
    self.a = a
    self.b = b
    def __cmp__(self, other):
    if self.a < other.a:
    return -1
    elif self.a > other.a:
    return 1
    else:
    return 0

    ?

    Thanks!
    Dan Stromberg, Sep 13, 2006
    #1
    1. Advertising

  2. Dan Stromberg

    Paul Rubin Guest

    Dan Stromberg <> writes:
    > def __cmp__(self, other):
    > if self.a < other.a:
    > return -1
    > elif self.a > other.a:
    > return 1
    > else:
    > return 0


    I think I'd have written

    return cmp(self.a, other.a)
    Paul Rubin, Sep 13, 2006
    #2
    1. Advertising

  3. Paul Rubin wrote:

    > Dan Stromberg <> writes:
    >> def __cmp__(self, other):
    >> if self.a < other.a:
    >> return -1
    >> elif self.a > other.a:
    >> return 1
    >> else:
    >> return 0

    >
    > I think I'd have written
    >
    > return cmp(self.a, other.a)


    /me agrees
    Richard Jones, Sep 13, 2006
    #3
  4. Dan Stromberg

    Aahz Guest

    In article <>,
    Dan Stromberg <> wrote:
    >
    >Is sample1 or sample2 the more pythonic way of comparing?


    Depending on what versions of Python you're targetting, some people
    would argue neither (advocating the deprecation of __cmp__). I'm not
    sure what the current status of __cmp__ is for Python 3.0.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "LL YR VWL R BLNG T S" -- www.nancybuttons.com
    Aahz, Sep 15, 2006
    #4
    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. gabor
    Replies:
    15
    Views:
    424
    Jorgen Grahn
    Sep 17, 2005
  2. rh0dium
    Replies:
    1
    Views:
    248
    George Sakkis
    Jan 18, 2006
  3. Carl J. Van Arsdall
    Replies:
    4
    Views:
    497
    Bruno Desthuilliers
    Feb 7, 2006
  4. Pythor

    More pythonic circle?

    Pythor, Apr 9, 2006, in forum: Python
    Replies:
    14
    Views:
    617
    John Machin
    Apr 10, 2006
  5. robert
    Replies:
    0
    Views:
    257
    robert
    Jun 3, 2006
Loading...

Share This Page