2

I am trying to figure out the shortest, most pythonic way to implement a similar to the following syntax:

if A and (B if C):
    print(A)

in a way that:

  • if C is False, then B is omitted (therefore (B if C) is True).
  • if C is True, then B is evaluated, effectively making the syntax if A and B:

This can be made through various separate if statements, but my ultimate purpose with this was to make it into a list comprehension for a value assignment.

Edit:

The list comprehension I wanted to make was this:

methods = [name for (name, func) in locals().items() \
    if callable(func) and (not __name__ == '__main__' or \
    func.__module__ == __name__)]

So that it returns the function names I have defined in that module as well as if methods is imported from the outside.

11
  • @chepner Right, thanks, it's not as simple. Commented Dec 14, 2018 at 16:29
  • if A and (B if C else True): or (A and B) if C else A: Commented Dec 14, 2018 at 16:29
  • If C is False, then the statement has to be true if A is true. But I admit I hadn't thought of that. @coldpseed Sorry for the late response, I see it was already responded... Commented Dec 14, 2018 at 16:30
  • A, B = True and C = False -> True and A,B,C = True -> True right? If so then it would be if A and not C or B: Commented Dec 14, 2018 at 16:31
  • 1
    @RonKalian Ah, I get what you're trying to say, but I meant that I still needed the "print" that I showed in my pseudocode, it's not just about evaluating the expressions. Commented Dec 14, 2018 at 16:55

5 Answers 5

5

This should be equivalent, if my old statement logic doesn't fail me =)

if A and (not C or B):
    print(A)

Explanation: "B if C" <=> C -> B <=> not C or B

Expression B is only evaluated if C holds.

Sign up to request clarification or add additional context in comments.

Comments

2

your hypothesis:

  • if C is False, then B is omitted
  • if C is True, then B is evaluated, effectively making the syntax if A and B:

wouldn't that be:

if A and (not C or B):
    print(A)
  • if C is false then not C is True and we don't evaluate B
  • if C is true, then not C is False, and we have to evaluate B

Comments

2

Your if pseudo-operator is just logical implication, where

C -> B = not C or B

This means you just want

if A and (not C or B):

When C is False, A and (not C or B) == A and (True or B) == A and True == A.

When C is True, A and (not C or B) == A and (False or B) == A and B.

1 Comment

Fun, irrelevant fact: some languages actually have logical implication. In Nix, for example, you could actually write A && (C -> B) as a Boolean expression instead of A && (!C || B). Whether that is more readable depends on how grounded you are in logic :)
2

This:

if A and (B if C else True):
    pass

is closest to your "pseudo code", using the conditional expression x if cond else y in Python. Assuming B=True in case C is False effectively make the if statement consider only the boolean value of A

1 Comment

I think this would actually be my preferred way to write it in Python.
1

I'd probably write it like this:

condition = (A and B) if C else A
if condition:
    print(A)

I've only broken the condition into a separate variable because I think mixing an if with a Python conditional expression looks a little confusing. You'll have to make a call whether it looks confusing when used inside a list comprehension or not.

Comments

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.