Live data from Hacker News

Code Like a Pythonista: Idiomatic Python

python.net

1–10 of 59 posts

Re: Code Like a Pythonista: Idiomatic Python

#2
I have just skimmed this so far, but it looks very good.

A lot of the Python scripts that I see at work look like either C programs or glorified batch files. I'll definitely point people to this when they are ready and willing to move on to the next level.

Well done.

Suggestion: one thing I didn't see mentioned is switching from "if s.find(c) == -1: ..." to "if c in s: ...". I see people do that a lot.

Re: Code Like a Pythonista: Idiomatic Python

#3
post #2

I have just skimmed this so far, but it looks very good. A lot of the Python scripts that I see at work look like either C programs or glorified batch files. I'll definitely point people to this when they are ready and willing to move on to the next level. Well done. Suggestion: one thing I didn't see mentioned is switching from "if s.find(c) == -1: ..." to "if c in s: ...". I see people do that a lot.

one thing I didn't see mentioned is switching from "if s.find(c) == -1: ..." to "if c in s: ...". I see people do that a lot.

I think this is covered implicitly by the two "Use in where possible" sections. Although as Tim Peter's The Zen of Python states, which is also quoted in the tutorial, "explicit is better than implicit." If people are doing that despite knowledge of the in keyword then I suppose that just proves Tim's point.

Re: Code Like a Pythonista: Idiomatic Python

#5
Read from the perspective of someone very much still learning to code, this was excellent. I particularly valued the idiomatic comparisons to 'naive' implementations (the kind I would initially reach for). One of the later sections on lazy generation also opened up some new doors for me, and gave me a nice solution to a problem I've been having with a piece of code.

Re: Code Like a Pythonista: Idiomatic Python

#6
post #2

I have just skimmed this so far, but it looks very good. A lot of the Python scripts that I see at work look like either C programs or glorified batch files. I'll definitely point people to this when they are ready and willing to move on to the next level. Well done. Suggestion: one thing I didn't see mentioned is switching from "if s.find(c) == -1: ..." to "if c in s: ...". I see people do that a lot.

You just made my weekend. I've been doing Python for a while and am well acquainted with the "item in list" idiom but did not know it could be used for find. That str.find syntax has always bothered me.

Still, I had to test just to be sure:

    test_cases = [
        # haystack, needle, expect
        ('abcdefg', 'a', True),
        ('abcdefg', 'b', True),
        ('abcdefg', 'bcd', True),
        ('abcdefg', 'h', False),
    ]
    
    for haystack,needle,expect in test_cases:
        # find version
        is_found = haystack.find(needle) != -1
    
        # in version
        is_in = needle in haystack
    
        # confirm
        print haystack, needle, expect, '-->', (is_found, is_in)
        assert is_found == expect
        assert is_found == is_in
Passed! Thanks.

Re: Code Like a Pythonista: Idiomatic Python

#8

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 essentially the same thing as "idiomatic python" in my opinion - I could pick apart different meanings from each but often times, it really is used to say the same thing.

The phrase "best practice" isn't taboo in my experience - I'll often ask what the "best practice would be for implementing XYZ". If you rephrase that, "what is an idiomatic way to implement XYZ", it's saying the same thing.

Python's community probably leans toward this diction because the culture of python revolves around the nebulous concepts of simplicity, obviousness, and "only one way", that in order to make those concepts more hylic and less "abstract and subjective" there is a lot of education (in the form of docs, PEP8, the very doc linked to in this post) to set the stage for what the expected idiom is.

Re: Code Like a Pythonista: Idiomatic Python

#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.

Re: Code Like a Pythonista: Idiomatic Python

#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
Post reply on HN