-2

I want to know if there is a more efficient way than making it ask: is it this? no? okay then is it this? no? okay then is it this? etc. I want it so that I can just say it is this so do that

if this = this:
    do this
elif this = that:
    do that
elif this = these
    do these
elif this = those
    do those

I want to be more efficient.

quamrana
  • 37,849
  • 12
  • 53
  • 71
Gyro161
  • 19
  • 4
  • 4
    It really depends on your actual problem. Maybe use a `dict` with functions. – Jan Aug 14 '20 at 09:20
  • 1
    This any help? https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python – alani Aug 14 '20 at 09:21

3 Answers3

2

Use a dictionary instead, assuming that this, that, these and those are functions:

def this():
    return "this"


def that():
    return "that"


def these():
    return "these"


def those():
    return "those"


d = {"this": this,
     "that": that,
     "these": these,
     "those": those
}

this = "that"

r = d.get(this, None)

print(r())
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
  • 2
    This executes all four branches to build a dictionary of strings. Normally you would want to put the functions in the dictionary. – kaya3 Aug 14 '20 at 09:29
  • 1
    In practise you would want to store the function names in your `dict`, not the result of running them once ever. – quamrana Aug 14 '20 at 09:30
  • Thanks for the feedback, I have modified the dictionary values to just the function names. – Gustav Rasmussen Aug 14 '20 at 09:35
  • 1
    When you try to `get` a value from the dictionary you might want to use a default return value of `None`. Then you can add a condition where you only call `r` if it's not `None`. – Matthias Aug 14 '20 at 09:44
0

You can create functions, store their names as values in a dictionary, with key corresponding to possible values your variable can take. Keys can be integers as well, here I've used string keys.

def mango(quantity):
    print("You selected "+str(quantity)+" mango(es).")

def banana(quantity):
    print("You selected "+str(quantity)+" banana(s).")

def apple():
    print("Here, have an apple")

fruits = {"m":mango, "b":banana}  #key->function name

fruit = "m"
quantity = 1 #e.g. of parameters you might want to supply to a funciton

if fruit in fruit_rates: #with if-else you can mimic 'default' case
    fruit_rates[fruit](quantity)
else:
    apple()
Deep
  • 329
  • 4
  • 13
0

The most efficient option really depends on what it is you're actually going for. Another option here would be ternary operators, which can be chained up

this() if this else that() if that else those() if those else these() if these

Depending on your code and use, you might be able to refactor it to use the shorthand ternary operator as well

this or that

...which will do the first thing that evaluates to true, but doesn't leave room for a separate condition. However, you can add a separate condition with

test and this or that

such that test and this both need to evaluate to true or else 'that' is evaluated. If 'this' and 'that' are both truthy expressions, 'test' behaves like your case.

If you'd like, you can also use truthiness to index into a tuple....

(do_if_false, do_if_true)[test]

this one, to me, is less readable and more voodoo, but 'test' effectively evaluates to 0 or 1, returning the expression at that index. However, this will also evaluate all of the expressions, unless you took an extra step with:

(lambda: do_if_false, lambda: do_if_true)[test]
Kyle Alm
  • 587
  • 3
  • 14