Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > What's more pythonic?

Reply
Thread Tools

What's more pythonic?

 
 
Dan Stromberg
Guest
Posts: n/a
 
      09-13-2006

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!

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      09-13-2006
Dan Stromberg <(E-Mail Removed)> 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)

 
Reply With Quote
 
 
 
 
Richard Jones
Guest
Posts: n/a
 
      09-13-2006
Paul Rubin wrote:

> Dan Stromberg <(E-Mail Removed)> 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
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      09-15-2006
In article <(E-Mail Removed)> ,
Dan Stromberg <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S" -- www.nancybuttons.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
DVD Verdict reviews: SYLVESTER AND THE MAGIC PEBBLE AND MORE MAGICAL TALES and more! DVD Verdict DVD Video 0 04-07-2005 08:10 AM
Sygate uses more and more memory? Louise Computer Security 0 06-01-2004 05:30 AM
Re: With More Flash More Lumix: using an external flash unit with the FZ1 and other digicams Hans-Georg Michna Digital Photography 4 08-24-2003 06:05 PM



Advertisments