What's more pythonic?

D

Dan Stromberg

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!
 
P

Paul Rubin

Dan Stromberg said:
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)
 
A

Aahz

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top