FAQ
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?

--
Filip Gruszczy?ski

Search Discussions

  • Rhodri James at Dec 4, 2009 at 12:44 am

    On Thu, 03 Dec 2009 15:41:56 -0000, Filip Gruszczy?ski wrote:

    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.
    I vaguely recall someone (Steven?) doing some timings that came up with
    the opposite answer. The list comprehension is probably the worst
    performing, since it creates a whole new list of the return values of
    choicesBox.addItem(choice), then throws it away.
    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?
    Your instincts are right, the explicit for loop is the more pythonic way
    in this case. More efficient or not, map() isn't a particularly pythonic
    way to do things, and there's no point using a list comprehension when you
    aren't building a list.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
  • MRAB at Dec 4, 2009 at 12:52 am

    Filip Gruszczy?ski wrote:
    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?
    Is .addItem() a function (returns a result) or a procedure (called
    for its side-effect, ie adding an item to a collection)?

    It's a procedure, so the first form is Pythonic.

    If it was a function then the third form would be Pythonic.

    The second form is what you would've done before list comprehensions
    were introduced.
  • Lie Ryan at Dec 4, 2009 at 2:46 am

    On 12/4/2009 11:44 AM, Rhodri James wrote:
    map(self.__choices, choicesBox.addItem)

    or

    [choicesBox.addItem(choice) for choice in self.__choices]
    Aside from being pythonic or non-pythonic, using map or list
    comprehension with a method with side-effect is not the intention of
    functional programming; which strives to eliminates side-effect
    altogether. So it's unfunctional.

    The only pythonic form to use method with side effect is an explicit for.
  • Edward A. Falk at Dec 4, 2009 at 3:07 am
    In article <mailman.1375.1259885328.2873.python-list at python.org>,
    Filip Gruszczy? ski wrote:
    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.
    --
    -Ed Falk, falk at despams.r.us.com
    http://thespamdiaries.blogspot.com/
  • Alex23 at Dec 4, 2009 at 3:20 am

    Filip Gruszczy?ski wrote:
    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.
  • Bruno Desthuilliers at Dec 4, 2009 at 11:26 am

    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 !-)
  • Nn at Dec 4, 2009 at 8:57 pm

    On Dec 3, 10:41?am, Filip Gruszczy?ski wrote:
    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?

    --
    Filip Gruszczy?ski
    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)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 3, '09 at 3:41p
activeDec 4, '09 at 8:57p
posts8
users8
websitepython.org

People

Translate

site design / logo © 2022 Grokbase