Code Like a Pythonista: Idiomatic Python
python.net
Code Like a Pythonista: Idiomatic Python
1–10 of 59 posts
Re: Code Like a Pythonista: Idiomatic Python
#2A 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
#3I 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.
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
#4Kidding aside, this was a great read.
Re: Code Like a Pythonista: Idiomatic Python
#5Re: Code Like a Pythonista: Idiomatic Python
#6I 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.
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
#7Re: Code Like a Pythonista: Idiomatic Python
#8In 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"?
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 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
#10In 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