473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

What's more pythonic?


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!

Sep 13 '06 #1
3 1043
Dan Stromberg <st******@dcs.nac.uci.eduwrites:
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)

Sep 13 '06 #2
Paul Rubin wrote:
Dan Stromberg <st******@dcs.nac.uci.eduwrites:
>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
Sep 13 '06 #3
In article <pa****************************@dcs.nac.uci.edu> ,
Dan Stromberg <st******@dcs.nac.uci.eduwrote:
>
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 (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"LL YR VWL R BLNG T S" -- www.nancybuttons.com
Sep 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: asdf sdf | last post by:
i need some advice. i'm a back end programmer historically, but have been exploring python for webapps and enjoying it. i need to build some simple Win client-server or standalone apps. the...
10
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary...
2
by: Marcus Goldfish | last post by:
I'd like advice/opinions on when it is appropriate to do attribute/property validation in python. I'm coming from a C#/Java background, where of course tons of "wasted" code is devoted to...
0
by: infidel | last post by:
http://dictionary.reference.com/search?q=pythonic http://thesaurus.reference.com/search?r=2&q=pythonic
20
by: krypto.wizard | last post by:
Is there any editor or IDE in Python (either Windows or Linux) which has very good debugging facilites like MS VisualStudio has or something like that. I like SPE but couldn't easily use winPDP....
3
by: jnair | last post by:
My Tead Lead my object counter code seen below is not pythonic class E(object): _count = 0 def __init__(self): E._count += 1 count = property(lambda self: E._count ) def test(): if...
16
by: Andy Dingley | last post by:
I'm trying to write rot13, but to do it in a better and more Pythonic style than I'm currrently using. What would you reckon to the following pretty ugly thing? How would you improve it? In...
25
by: Alex Popescu | last post by:
Hi all! I am pretty sure this has been asked a couple of times, but I don't seem to find it on the archives (Google seems to have a couple of problems lately). I am wondering what is the most...
26
by: Frank Samuelson | last post by:
I love Python, and it is one of my 2 favorite languages. I would suggest that Python steal some aspects of the S language. ------------------------------------------------------- 1. Currently...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.