idioms

Python indentation in "empty lines"

Python indentation in "empty lines" Question: Which is preferred (“.” indicating whitespace)? A) def foo(): x = 1 y = 2 …. if True: bar() B) def foo(): x = 1 y = 2 if True: bar() My intuition would be B (that’s also what vim does for me), but I see people using A) …

Total answers: 10

Idiomatic Python: 'times' loop

Idiomatic Python: 'times' loop Question: Say I have a function foo that I want to call n times. In Ruby, I would write: n.times { foo } In Python, I could write: for _ in xrange(n): foo() But that seems like a hacky way of doing things. My question: Is there an idiomatic way of …

Total answers: 4

Python "Every Other Element" Idiom

Python "Every Other Element" Idiom Question: I feel like I spend a lot of time writing code in Python, but not enough time creating Pythonic code. Recently I ran into a funny little problem that I thought might have an easy, idiomatic solution. Paraphrasing the original, I needed to collect every sequential pair in a …

Total answers: 8

Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

Is str.replace(..).replace(..) ad nauseam a standard idiom in Python? Question: For instance, say I wanted a function to escape a string for use in HTML (as in Django’s escape filter): def escape(string): “”” Returns the given string with ampersands, quotes and angle brackets encoded. “”” return string.replace(‘&’, ‘&amp;’).replace(‘<‘, ‘&lt;’).replace(‘>’, ‘&gt;’).replace(“‘”, ‘&#39;’).replace(‘”‘, ‘&quot;’) This works, but …

Total answers: 9

Are one-line 'if'/'for'-statements good Python style?

Are one-line 'if'/'for'-statements good Python style? Question: Every so often on here I see someone’s code and what looks to be a ‘one-liner’, that being a one line statement that performs in the standard way a traditional ‘if’ statement or ‘for’ loop works. I’ve googled around and can’t really find what kind of ones you …

Total answers: 10

What is the pythonic way to detect the last element in a 'for' loop?

What is the pythonic way to detect the last element in a 'for' loop? Question: How can I treat the last element of the input specially, when iterating with a for loop? In particular, if there is code that should only occur "between" elements (and not "after" the last one), how can I structure the …

Total answers: 34

Alternative to the `match = re.match(); if match: …` idiom?

Alternative to the `match = re.match(); if match: …` idiom? Question: If you want to check if something matches a regex, if so, print the first group, you do.. import re match = re.match(“(d+)g”, “123g”) if match is not None: print match.group(1) This is completely pedantic, but the intermediate match variable is a bit annoying.. …

Total answers: 10

Python: most idiomatic way to convert None to empty string?

Most idiomatic way to convert None to empty string? Question: What is the most idiomatic way to do the following? def xstr(s): if s is None: return ” else: return s s = xstr(a) + xstr(b) update: I’m incorporating Tryptich’s suggestion to use str(s), which makes this routine work for other types besides strings. I’m …

Total answers: 17

What does if __name__ == "__main__": do?

What does if __name__ == "__main__": do? Question: What does this do, and why should one include the if statement? if __name__ == "__main__": print("Hello, World!") If you are trying to close a question where someone should be using this idiom and isn’t, consider closing as a duplicate of Why is Python running my module …

Total answers: 47

LBYL vs EAFP in Java?

LBYL vs EAFP in Java? Question: I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to error checking before code execution. In Python, it seems the accepted style is EAFP, and it seems to work well with the language. LBYL (Look Before You Leap): def safe_divide_1(x, y): if y == 0: …

Total answers: 5