idioms

Multiple constructors: the Pythonic way?

Multiple constructors: the Pythonic way? Question: I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don’t pass data; just create an empty container In Java, I would create three constructors. Here’s …

Total answers: 7

when to use if vs elif in python

when to use if vs elif in python Question: If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function: def example(x): if x > 0: return ‘positive’ if x < 0: return …

Total answers: 7

The preferred way to set matplotlib figure/axes properties

The preferred way to set matplotlib figure/axes properties Question: Say I have a matplotlib axes called ax, and I want to set several of its properties. Currently, I do it like this: ax.set_yscale(‘log’) ax.set_xlim([0,10]) ax.set_xlabel(‘some label’) But it gets tedious after a while. Then I ran into this method: ax.set(yscale=’log’, xlim=[0,10], xlabel=’some label’) Much more …

Total answers: 4

Python negative zero slicing

Python negative zero slicing Question: I often find myself having to work with the last n items in a sequence, where n may be 0. The problem is that trying to slice with [-n:] won’t work in the case of n == 0, so awkward special case code is required. For example if len(b): assert(isAssignableSeq(env, …

Total answers: 5

Union of dict objects in Python

Union of dict objects in Python Question: How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {‘a’ : 0, ‘b’ : 1} and {‘c’ : 2} is …

Total answers: 4

How do I use Python to easily expand variables to strings?

How do I use Python to easily expand variables to strings? Question: What’s a nice idiom to do this: Instead of: print “%s is a %s %s that %s” % (name, adjective, noun, verb) I want to be able to do something to the effect of: print “{name} is a {adjective} {noun} that {verb}” Asked …

Total answers: 5

Pairs from single list

Pairs from single list Question: Often enough, I’ve found the need to process a list by pairs. I was wondering which would be the pythonic and efficient way to do it, and found this on Google: pairs = zip(t[::2], t[1::2]) I thought that was pythonic enough, but after a recent discussion involving idioms versus efficiency, …

Total answers: 10

`if key in dict` vs. `try/except` – which is more readable idiom?

`if key in dict` vs. `try/except` – which is more readable idiom? Question: I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do …

Total answers: 11

How to count non-null elements in an iterable?

How to count non-null elements in an iterable? Question: I’m looking for a better/more Pythonic solution for the following snippet count = sum(1 for e in iterable if e) Asked By: Alexandru || Source Answers: Honestly, I can’t think of a better way to do it than what you’ve got. Well, I guess people could …

Total answers: 9

Get the key corresponding to the minimum value within a dictionary

Get the key corresponding to the minimum value within a dictionary Question: If I have a Python dictionary, how do I get the key to the entry which contains the minimum value? I was thinking about something to do with the min() function… Given the input: {320:1, 321:0, 322:3} It would return 321. Asked By: …

Total answers: 16