Python Forum
if stack variations ... pythonic consideration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if stack variations ... pythonic consideration
#1
i would like to know if these six code samples would be different enough to consider which would be the best performer and if this would be a pythonic consideration. if so, which is most pythonic? the condN function calls are just sample place holders for various condition expressions in each. there will typically be more than three conditions (probably at least six but i didn't want to make this post that big).

#1
# cond1, cond2, and cond3 are mutually exclusive.
# no more than one can be true for the same value.
def my_function(a):
    if cond1(a):
        return yyy1(xxx1(a))
    if cond2(a):
        return yyy2(xxx2(a))
    if cond3(a):
        return yyy3(xxx3(a))
    return None
#2
# cond1, cond2, and cond3 are mutually exclusive.
# no more than one can be true for the same value.
def my_function(a):
    if cond1(a):
        return yyy1(xxx1(a))
    elif cond2(a):
        return yyy2(xxx2(a))
    elif cond3(a):
        return yyy3(xxx3(a))
    return None
#3
# cond1, cond2, and cond3 are mutually exclusive.
# no more than one can be true for the same value.
def my_function(a):
    if cond1(a):
        foo = yyy1(xxx1(a))
    if cond2(a):
        foo = yyy2(xxx2(a))
    if cond3(a):
        foo = yyy3(xxx3(a))
    else:
        foo = None
    return foo
#4
# cond1, cond2, and cond3 are mutually exclusive.
# no more than one can be true for the same value.
def my_function(a):
    if cond1(a):
        foo = yyy1(xxx1(a))
    elif cond2(a):
        foo = yyy2(xxx2(a))
    elif cond3(a):
        foo = yyy3(xxx3(a))
    else:
        foo = None
    return foo
#5
# cond1, cond2, and cond3 are mutually exclusive.
# no more than one can be true for the same value.
def my_function(a):
    foo = None
    if cond1(a):
        foo = yyy1(xxx1(a))
    if cond2(a):
        foo = yyy2(xxx2(a))
    if cond3(a):
        foo = yyy3(xxx3(a))
    return foo
#6
# cond1, cond2, and cond3 are mutually exclusive.
# no more than one can be true for the same value.
def my_function(a):
    foo = None
    if cond1(a):
        foo = yyy1(xxx1(a))
    elif cond2(a):
        foo = yyy2(xxx2(a))
    elif cond3(a):
        foo = yyy3(xxx3(a))
    return foo
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
#2 is correct although the return None at the end is not pythonic: you can simply remove it. Most of the time I'd use #4 because I think too many return statements is confusing. elif is the pythonic way to go if the conditions are mutually exclusive.
Reply
#3
I would agree with Gribouillis. The point is short-circuiting. The returns in #2 make sure that no item is checked after a true value is found, as the do the elifs in #4. So you are not wasting time checking things that shouldn't be true. Also, short circuiting works best if the most likely choice is tested first. So if cond2 is significantly more likely than cond1, you should test cond2 first (for maximum efficiency).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Just a mental exercise for Saturday night (won't work if function result is something which evaluates as False: 0, emtpy string/list etc):

result = (cond1(a) and yyy1(xxx1(a))) or (cond2(a) and yyy2(xxx2(a))) or (cond3(a) and yyy3(xxx3(a)))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Nov-23-2019, 05:18 PM)perfringo Wrote: Just a mental exercise for Saturday night (won't work if function result is something which evaluates as False: 0, emtpy string/list etc):

result = (cond1(a) and yyy1(xxx1(a))) or (cond2(a) and yyy2(xxx2(a))) or (cond3(a) and yyy3(xxx3(a)))

the actual conditions can be rather long expressions (60+ characters). the yyyN() functions might be returning None.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020