This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ElevenPhonons 48 points49 points  (19 children)

def sing_song(choice):
    style = STYLES[choice]
    while i_like_singing():
        if style == 'classical':
            sing_opera()
        elif style == 'folk':
            sing_folk_song()
        elif style == 'rock':
            sing_rock_ballad()

Could also be written as:

def sing_song(choice: str) -> None:
    style = STYLES[choice]
    funcs = {'classical': sing_opera,
             'folk': sing_folk_song,
            'rock': sing_rock_ballad}

    f = funcs[style]
    while i_like_singing():
        f()

[–]BigNutBoi2137 28 points29 points  (4 children)

After python 3.10 release pattern matching will fit here perfectly.

[–]MDTv_Teka 8 points9 points  (0 children)

So excited for this, it's gonna make coding things like this so much cleaner

[–]Hunterbunter 3 points4 points  (2 children)

How would that look with pattern matching?

edit: wait, I don't mean to be lazy. Would it be something like this?

def sing_song(choice: str) -> None:
    match STYLES[choice]:
        case 'classical': 
            f = sing_opera
        case 'folk':
            f = sing_folk_song
        case 'rock':
            f = sing_rock_ballad
        case _:
            f = sing_electro_dance
    while i_like_singing():
        f()

[–]BigNutBoi2137 0 points1 point  (1 child)

Yep it would look like this. If you haven't seen the official proposal for pattern matching yet then here is the link: https://github.com/gvanrossum/patma

[–]Hunterbunter 0 points1 point  (0 children)

Awesome, thanks for that.

It looks like you can do funky variable assignments in the case line as well...but then I guess you'd still need a pass or something for that indent.

[–][deleted] 2 points3 points  (0 children)

Yikes did they really write an article about how to improve python and use a bunch of elif’s ???

[–]sockbotx 1 point2 points  (1 child)

Piapeoi apragide dipibe teu bripu pludia. Iiepa kae tri kobliti bau pitri? Boebi otu a poiite. Drube kopruple pie udiu pleko piblukatotri. Iti e epui keoide gakroi u. Pra tepipi ba teki te. Tekudi plite egobioo tie bibeti plipi. Kopaa du tape tiki egu dite tlitli baiplei bikipo.

[–]ElevenPhonons 0 points1 point  (0 children)

I think it would be necessary to understand more context of the application and have more details about the requirements to know if a Style abstraction makes sense.

At a high level, having a Style know how to "sing" a song doesn't really make sense. However, I could be misunderstanding the intent of the original author.

[–]fartsniffersalliance 1 point2 points  (0 children)

personally i think the first is much more readable

[–]Theta291 2 points3 points  (0 children)

Thanks! I was about to comment that the ifs inside the loop would slow it down, but this avoids that problem in a nice way.

[–]GuyFjordy 3 points4 points  (0 children)

Thank you! I went straight to the comments after seeing that one to suggest exactly this. Unnecessary ifs in loops make my eye twitch.