osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Switch function


Message asking about a fairly simple way to implement a switch in python as
per the ongoing discussion.

 

I wrote a function that might emulate a fairly simple general use of switch.

 

A function would take N+2 arguments of the form:

1: something to switch based on

2,3: something to match to the first and if matched, return the second.

4,5: another pair of match and response

.

N: an OPTIONAL singleton to serve as a default.

 

The function will be shown next but in English, it accepts the one to search
for and pairs of key/value to compare and if matched return. As soon as
anything matches, return it. If all matches fail and if there is one
argument left, return that. Else, return None.

 

Now this should work well only with relatively fixed objects that are
evaluated whether used or not. Again, just a suggestion to discuss, not
necessarily something to implement although this seems trivial enough that
something like it is likely out there.

 

def switcheroo(*args):

        theOne, *rest = args

 

        while len(rest) > 1:

            matching, returning, *rest = rest

            if theOne == matching:

                return returning

        if rest: # default

            return rest[0]

        else:

            return None

 

I used python 3.7.1 so note the *rest notation may not work on very old
versions. You can use other methods like popping off a list though.

 

Now for the specific case of days 1:31, here is how you can call it:

 

>>> switcheroo(3,

                       1, "st",

                       2, "nd",

                       3, "rd",

                       21, "st",

                       31, "st",

                       22, "nd",

                       23, "rd",

                       "th" )

                                

'rd'

 

With no default, you get an invisible None.

 

Since normal people don't like to type that way, here is how you might use
it.

 

>>> pairs =          [1,"st", 2,"nd", 3,"rd", 21,"st", 31,"st", 22,"nd",
22,"nd", 23,"rd"]

                                

>>> switcheroo(2, *pairs, "th")

                                

'nd'

>>> switcheroo(5, *pairs, "th")

                                

'th'

 

You can make many variations on this theme including using dictionaries and
the **kwargs notation, or moving the default up near the front and having a
None placeholder. As far as I know, nothing stops the things being matched
or returned from being objects of any kind or functions.

 

Final note. I repeat. This method has all arguments evaluated before the
function is called even if they are not the ones being matched. There is no
delayed lazy evaluation as in R. See the following examples:

 

>>> switcheroo(1, -int((math.e**(math.pi*complex(0,1))).real), "st",

                       2, "nd",

                       1+2, "rd",

                       21, "st",

                       31, "st",

                       2*11, "nd",

                       23, "rd",

                       "th"

                       )

                                

'st'

>>> switcheroo(23, -int((math.e**(math.pi*complex(0,1))).real), "st",

                       2, "nd",

                       1+2, "rd",

                       21, "st",

                       31, "st",

                       2*11, "nd",

                       23, "rd",

                       "th"

                       )

                                

'rd'

 

Bottom line, does anyone bother using anything like this? It is actually a
bunch of hidden IF statements matched in order but may meet many needs.