Most pythonic way to call a list of functions [closed]

2024/11/24 2:40:04

I have a list of functions, like so:

def foo(a, b):# do stuffreturn True # or Falsedef bar(c):# do stuffreturn False # or Truedef baz(d, e, f):# do stuffreturn True # or False

and I want to call each of them in turn, and only proceed to the next one if the previous one returns True, like so:

if foo(1, 2):if bar(3):if baz(4, 5, 6):print("Success!")

Now, I know that if all of my functions accepted the same arguments, I could do something like:

steps = [foo, bar, baz]
if all(func(*args) for func in steps):print("Success!")

because all would short-circuit as soon as it reaches a False return value. However, they do not all accept the same arguments.

What would the cleanest way to achieve this?

Edit: Thanks for the suggestions about zipping the args. What about a more general case where each function returns some value, and I want to use the return value of the previous function, whatever it may be, to decide whether or not I run the next function? Each function may also require as arguments some of the return values of the previous functions.

Answer

The and operator also short-circuits, so you can do:

if foo(1, 2) and bar(3) and baz(4, 5, 6):print("Success")

If you want a more general way, you can make a list of lambdas.

steps = [lambda: foo(1, 2), lambda: bar(3), lambda: baz(4, 5, 6)
]
if all(f() for f in steps):print("Success")

or a list of tuples:

steps = [(foo, (1, 2)), (bar, (3)), (baz, (4, 5, 6))
]
if all(f(*args) for f, args in steps):print("Success")
https://en.xdnf.cn/q/120582.html

Related Q&A

Unable to get wanted output using for and range functions

For my homework assignment, I am using the for loop and the range function. I have to create a loop that printsHello 0 Hello 1 Hello 3 Hello 6 Hello 10The question says that the number corresponds to t…

i want to add a new field to an existing module odoo11 but i dont know why it didnt work

product_template.xmlthe view to add the customizing field to the product module<?xml version="1.0" encoding="utf-8"?><odoo><data><record id="product_temp…

How to check with more RegEx for one address in python using re.findall()

How to check with more RegEx for one address in python using re.findall()Ex: I want to apply the below regex rules # need to get addresstxt = "hello user 44 West 22nd Street, New York, NY 12345 fr…

Finding all roots of an equation in Python

I have a function that I want to find its roots. I could write a program to figure out its roots but the point is, each time that I want to find the other root I should give it an initial value manuall…

defining matrix class in python

Define a class that abstracts the matrix that satisfies the following examples of practice

Why do round() and math.ceil() give different values for negative numbers in Python 3? [duplicate]

This question already has an answer here:What is the algorithmic difference between math.ceil() and round() when trailing decimal points are >= 0.5 in Python 3?(1 answer)Closed 6 years ago.Why do r…

getting the value from text file after the colon or before the colon in python

I only need the last number 25 so i can change it to int(). I have this text file:{"members": [{"name": "John", "location": "QC", "age": 25}…

How to remove the background of an object using OpenCV (Python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to move zeros to the end of a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

My program only appends one user input to list when main is looped

This is part of my code for a guessing game. I want to count the guesses of a player, and then append their name and number of guesses to a list that is later written or appended to file. As of now, it…