Live data from Hacker News

Code Like a Pythonista: Idiomatic Python

python.net

11–20 of 59 posts

Re: Code Like a Pythonista: Idiomatic Python

#11
post #9

In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.

[deleted]

Re: Code Like a Pythonista: Idiomatic Python

#12
post #10
post #9

In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.

x = [0] * 100

Thanks! At first I wondered about efficiency but it seems like I can allocate lists with tens of millions of elements this way without any spike in CPU so it is obviously implemented efficiently. I knew there would be a better way :-)

Re: Code Like a Pythonista: Idiomatic Python

#13

In the Java ecosystem, they call it best practices. But of course using the phrase "best practices" these days is considered taboo. Thus the use of the word "idiomatic"?

A "best practice" is an optimal way to do something in a given context. An "idiom" is a commonly accepted way to express the thing you want to do. Best practices and idioms overlap, but are not identical.

Re: Code Like a Pythonista: Idiomatic Python

#14
post #9

In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.

Yeah, that is kludgy (you're creating two lists now!).

First thing I would do is ask why I need that; I've never come across a case where I needed a pre-built list of items all initialized to a common or empty value - if you're doing that, I would explore some sort of custom generative process (building the items of your list as you need them). But, just because I can't think of a good use case for this (where I couldn't use generative recursion or iteration) doesn't mean you haven't. Weigh what you are doing.

I would do this one of two ways: create a function that will produce an object that will generatively build the list (like xrange, but pass an "initializer" value, like myxrange(0, 100, initialize=0)). See the source for Python's xrange to do this it should be quite easy.

Or do this in a while loop (flat, easy to understand, and you aren't being too naughty by producing more than once list):

    ls  = []
    cnt = 0
    
    while cnt 
Alternatively you can do the above with a for loop but you would have to iterate over a sequence (which means creating a list to do it); the while loop is going to be the most efficacious way of doing what you want I think, excluding building a custom xrange style implementation.

Re: Code Like a Pythonista: Idiomatic Python

#15
post #10
post #9

In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.

x = [0] * 100

This is much better than my own comment - I never even knew about that syntax!

Is it efficient though?

Re: Code Like a Pythonista: Idiomatic Python

#16

In the Java ecosystem, they call it best practices. But of course using the phrase "best practices" these days is considered taboo. Thus the use of the word "idiomatic"?

http://dictionary.reference.com/browse/idiom

An idiom is rather a preferred practice.

If you take some time to read through some Python source, you'll notice that, although the code uses the exact same constructs that you are used to in Java, it does so in a manner you might find unfamiliar or unpredictable.

One example is the use of try/except instead of if/else (beg for forgiveness rather than ask permission). Another is Duck-typing rather than Interface (if it quacks like a duck...)

Coming from Java and looking at some Python code without forewarning or context, you might just be tempted to label the whole thing as really really bad practice, when in truth, it's just Idiomatic Python.

Re: Code Like a Pythonista: Idiomatic Python

#17
post #14
post #9

In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.

Yeah, that is kludgy (you're creating two lists now!). First thing I would do is ask why I need that; I've never come across a case where I needed a pre-built list of items all initialized to a common or empty value - if you're doing that, I would explore some sort of custom generative process (building the items of your list as you need them). But, just because I can't think of a good use case for this (where I coul…

Actually [x for x in xrange(..)] will produce only one list. In Python 2.X, range() returns a list while xrange() an iterable.

If you just need an iterable, itertools.repeat(0, 100) will do the trick.

Re: Code Like a Pythonista: Idiomatic Python

#18
post #15
post #10

Earlier quoted context omitted.

x = [0] * 100

This is much better than my own comment - I never even knew about that syntax! Is it efficient though?

My guess is that it's optimized, but I don't know. Semantics-wise, it's consistent with list addition.

    >>> [0] + [0] + [0]
    [0, 0, 0]
    >>> [0] * 3
    [0, 0, 0]

Re: Code Like a Pythonista: Idiomatic Python

#19
post #17
post #14

Earlier quoted context omitted.

Yeah, that is kludgy (you're creating two lists now!). First thing I would do is ask why I need that; I've never come across a case where I needed a pre-built list of items all initialized to a common or empty value - if you're doing that, I would explore some sort of custom generative process (building the items of your list as you need them). But, just because I can't think of a good use case for this (where I coul…

Actually [x for x in xrange(..)] will produce only one list. In Python 2.X, range() returns a list while xrange() an iterable. If you just need an iterable, itertools.repeat(0, 100) will do the trick.

I suppose I didn't say it correctly, you're still maintaining two objects though (xrange the iterable, and the new list being built by the comprehension). The "optimization" I suppose is a moot point because Python's handling of lists is pretty solid and this isn't the 90's anymore, but at least with the while loop you've only got one list object and an integer (which is faster, I would think, to increment than it is to produce the next sequence in the iterable object?).

Anywho, splitting hairs. Goladus had the most helpful comment (I even learned something).

Re: Code Like a Pythonista: Idiomatic Python

#20
post #16

In the Java ecosystem, they call it best practices. But of course using the phrase "best practices" these days is considered taboo. Thus the use of the word "idiomatic"?

http://dictionary.reference.com/browse/idiom An idiom is rather a preferred practice. If you take some time to read through some Python source, you'll notice that, although the code uses the exact same constructs that you are used to in Java, it does so in a manner you might find unfamiliar or unpredictable. One example is the use of try/except instead of if/else (beg for forgiveness rather than ask permission). Anot…

To be fair, though, some of the things on that list really do seem more like best practices, like advice not to use:

    from module import *
Post reply on HN