Is there a neater alternative to `except: pass`?

2024/10/7 10:13:47

I had a function that returned a random member of several groups in order of preference. It went something like this:

def get_random_foo_or_bar():"I'd rather have a foo than a bar."if there_are_foos():return get_random_foo()if there_are_bars():return get_random_bar()raise IndexError, "No foos, no bars"

However, the first thing get_random_foo does is verify there are foos and raise an IndexError if not, so there_are_foos is redundant. Moreover, a database is involved and using separate functions creates a concurrency issue. Accordingly, I rewrote it something like this:

def get_random_foo_or_bar():"Still prefer foos."try:return get_random_foo()except IndexError:passtry:return get_random_bar()except IndexError:passraise IndexError, "No foos, no bars"

But I find this much less readable, and as I've never had reason to use pass before it feels instictively wrong.

Is there a neater efficient pattern, or should I learn to accept pass?

Note: I'd like to avoid any nesting since other types may be added later.


Edit

Thanks everyone who said that pass is fine - that's reassuring!

Also thanks to those who suggested replacing the exception with a return value of None. I can see how this is a useful pattern, but I would argue it's semantically wrong in this situation: the functions have been asked to perform an impossible task so they should raise an exception. I prefer to follow the behaviour of the random module (eg. random.choice([])).

Answer

That is exactly how I would write it. It's simple and it makes sense. I see no problem with the pass statements.

If you want to reduce the repetition and you anticipate adding future types, you could roll this up into a loop. Then you could change the pass to a functionally-equivalent continue statement, if that's more pleasing to your eyes:

for getter in (get_random_foo, get_random_bar):try:return getter()except IndexError:continue  # Ignore the exception and try the next type.raise IndexError, "No foos, no bars"
https://en.xdnf.cn/q/70252.html

Related Q&A

Get a permutation as a function of a unique given index in O(n)

I would like to have a function get_permutation that, given a list l and an index i, returns a permutation of l such that the permutations are unique for all i bigger than 0 and lower than n! (where n …

How to generate Bitcoin keys/addresses from a seed in Python?

I am trying to create a set of public/private keys from a mnemonic based on BIP0039. I am working in Python.Here is the code I have so far:from mnemonic import Mnemonic mnemon = Mnemonic(english) words…

NumPy - Set values in structured array based on other values in structured array

I have a structured NumPy array:a = numpy.zeros((10, 10), dtype=[("x", int),("y", str)])I want to set values in a["y"] to either "hello" if the corresponding val…

numpy.disutils.system_info.NotFoundError: no lapack/blas resources found

Problem: Linking numpy to correct Linear Algebra libraries. Process is so complicated that I might be looking for the solution 6th time and I have no idea whats going wrong. I am on Ubuntu 12.04.5. I …

Opencv: Jetmap or colormap to grayscale, reverse applyColorMap()

To convert to colormap, I doimport cv2 im = cv2.imread(test.jpg, cv2.IMREAD_GRAYSCALE) im_color = cv2.applyColorMap(im, cv2.COLORMAP_JET) cv2.imwrite(colormap.jpg, im_color)Then,cv2.imread(colormap.jpg…

Get file modification time to nanosecond precision

I need to get the full nanosecond-precision modified timestamp for each file in a Python 2 program that walks the filesystem tree. I want to do this in Python itself, because spawning a new subprocess …

`TypeError: argument 2 must be a connection, cursor or None` in Psycopg2

I have a heroku pipeline set up, and have just enabled review apps for it. It is using the same codebase as my staging and production apps, same settings files and everything.When the review app spins …

replace string if length is less than x

I have a dataframe below. a = {Id: [ants, bees, cows, snakes, horses], 2nd Attempts: [10, 12, 15, 14, 0],3rd Attempts: [10, 10, 9, 11, 10]} a = pd.DataFrame(a) print (a)I want to able add text (-s) to …

Convert ast node into python object

Given an ast node that can be evaluated by itself, but is not literal enough for ast.literal_eval e.g. a list comprehensionsrc = [i**2 for i in range(10)] a = ast.parse(src)Now a.body[0] is an ast.Expr…

Keras custom loss function (elastic net)

Im try to code Elastic-Net. Its look likes:And I want to use this loss function into Keras:def nn_weather_model():ip_weather = Input(shape = (30, 38, 5))x_weather = BatchNormalization(name=weather1)(ip…