2

I have this simple code here:

def myFunction(a: my_enumA, b: my_enumB, c: my_enumC):
  
  if a:
    update_a = update_from_db_a(a=a)
  else:
    update_a = True
  
  if b:
    update_b = update_from_db_b(b=b)
  else:
    update_b = True
  
  if c:
    update_c = update_from_db_c(c=c)
  else:
    update_c = True
    
  if update_a and update_b and update_c:
    return True
  else:
    return False

I'm sure that a design patern exists for this but I don't know the name.

What is the best way Pythonic way to implement it ? Maybe with a design pattern?

4
  • 1
    Isn't that equivalent to just returning not(a or b or c) ? What do the update_from_db_X return ? Commented Mar 7, 2022 at 14:21
  • 1
    I question the need for this kind of wrapper function in the first place. Let the caller decide whether the appropriate update functions need to be called, then pass a list like [update_from_db_a(v1), True, update_from_db_c(v3)] to all. Commented Mar 7, 2022 at 14:22
  • That was my first idea. update_from_db must be done to save some data in the database. In your example, if b is false, it won't try to save the data for c. Commented Mar 7, 2022 at 14:23
  • The function update make a query to save some data and return Trueif it works or False if it does not work Commented Mar 7, 2022 at 14:45

2 Answers 2

3

You could transform each of your first ifs into a single statement:

# If a is False, value is True
# If a is True, value is update_from_db_a(a=a)
update_a = not a or update_from_db_a(a=a)

Notice that if not a is True (a.k.a a is False) it won't execute the next part of the condition (it won't call update_from_db_a).

Also, your function will return True only if a condition is True, so you could re-write it like this:

# If condition is True, returns True
# If condition is False, returns False
return update_a and update_b and update_c

Resulting code:

def myFunction(a: my_enumA, b: my_enumB, c: my_enumC):
    update_a = not a or update_from_db_a(a=a)
    update_b = not b or update_from_db_b(b=b)
    update_c = not c or update_from_db_c(c=c)
    return update_a and update_b and update_c
Sign up to request clarification or add additional context in comments.

1 Comment

"Also, your function will return True only if a condition is True, so you could re-write it like this:" No I can't. I need to go to update_from_db_a, if a = True but also if in update_from_db_b if b = true
1

You can compress all conditions and pass them to all():

def myFunction(a: my_enumA, b: my_enumB, c: my_enumC):
    return all([
        not a or update_from_db_a(a=a),
        not b or update_from_db_b(b=b),
        not c or update_from_db_c(c=c)
    ])

Be sure to wrap them in an iterable, such as a list, tuple or set, and not in a generator. Otherwise all() will terminate on the first false condition.

2 Comments

Why a set in particular? I think a tuple or list would do the job too. The important part is to avoid using a generator right?
Right. I chose a set out of habit, because on large sets of conditions it only stores one instance of True or False and might accelerate all(). But you can also use a tuple or a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.