Which is more pythonic?

  • Thread starter Filip GruszczyÅ„ski
  • Start date
F

Filip Gruszczyński

I have just written a very small snippet of code and started thinking,
which version would be more pythonic. Basically, I am adding a list of
string to combo box in qt. So, the most obvious way is:

for choice in self.__choices:
choicesBox.addItem(choice)

But I could also do:

map(self.__choices, choicesBox.addItem)

or

[choicesBox.addItem(choice) for choice in self.__choices]

I guess map version would be fastest and explicit for is the slowest
version. However, the first, most obvious way seems most clear to me
and I don't have to care about speed with adding elements to combo
box. Still, it's two lines instead of one, so maybe it's not the best.
So, which one is?
 
E

Edward A. Falk

for choice in self.__choices:
choicesBox.addItem(choice)

This is the easiest to read. I'm guessing that this is not inner-loop
stuff that needs to be optimized, so you should favor readability over
performance.
 
A

alex23

Filip Gruszczyñski said:
I guess map version would be fastest and explicit for is the slowest
version. However, the first, most obvious way seems most clear to me
and I don't have to care about speed with adding elements to combo
box. Still, it's two lines instead of one, so maybe it's not the best.
So, which one is?

Both map and the listcomp create & modify a list, which seems kind of
odd to do if you're just throwing the list away afterward.

What's more important than a low line count is clarity of intent. The
for-loop makes it clear to me that it's the action against each item
that's important, not the resulting list of processed items.
 
B

Bruno Desthuilliers

Filip Gruszczyński a écrit :
I have just written a very small snippet of code and started thinking,
which version would be more pythonic. Basically, I am adding a list of
string to combo box in qt. So, the most obvious way is:

for choice in self.__choices:
choicesBox.addItem(choice)

But I could also do:

map(self.__choices, choicesBox.addItem)

this should actually be
map(choicesBox.addItem, self.__choices)

!-)
or

[choicesBox.addItem(choice) for choice in self.__choices]

I guess map version would be fastest and explicit for is the slowest
version.

I don't think so - there's at least the overhead of creating a useless
list. But if you're after micro-optimization, there's an obvious one :

addItem = choicesBox.addItem
for choice in self.__choices:
addItem(choice)

Attribute lookup can be costly, specially when the attribute is a method.

Now unless you have a _very_ big choices list - which is probably not
the case - the gain will still be marginal.
However, the first, most obvious way seems most clear to me

It is.
and I don't have to care about speed with adding elements to combo
box. Still, it's two lines instead of one, so maybe it's not the best.
So, which one is?

The first, obviously - and I'm the kind of guy that really dig obscure
one-liners !-)
 
N

nn

I have just written a very small snippet of code and started thinking,
which version would be more pythonic. Basically, I am adding a list of
string to combo box in qt. So, the most obvious way is:

for choice in self.__choices:
        choicesBox.addItem(choice)

But I could also do:

map(self.__choices, choicesBox.addItem)

or

[choicesBox.addItem(choice) for choice in self.__choices]

I guess map version would be fastest and explicit for is the slowest
version. However, the first, most obvious way seems most clear to me
and I don't have to care about speed with adding elements to combo
box. Still, it's two lines instead of one, so maybe it's not the best.
So, which one is?

First option is the most pythonic IMO. If it HAS to be one line I
would still prefer:

for choice in self.__choices: choicesBox.addItem(choice)
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top