Implement C's Switch in Python 3

Stefan Behnel stefan_ml at behnel.de
Sun Feb 3 03:29:28 EST 2019


Chris Angelico schrieb am 03.02.19 um 02:23:
> Of course, you can also precompute this:
> 
> day_ordinal = mapper(
>         [1, 21, 31], "st",
>         [2, 22], "nd",
>         [3, 23], "rd",
> )
> def f(x): return day_ordinal.get(x, "th")

… in which case I would also 'precompute' the ".get" and give the resulting
callable a properly readable name all-together:

    find_special_day_ordinal = mapper(
        [1, 21, 31], "st",
        [2, 22], "nd",
        [3, 23], "rd",
    ).get

    print(find_special_day_ordinal(x, "th"))

Stefan




More information about the Python-list mailing list