I wish Python had…

Yes, there are plenty of wish lists for Python already, but I’m going to make another one.

I wish Python had a switch block like JavaScript does. switch drastically reduces the number of lines, conditions, and effort of setting up multiple if, else if, and else blocks, and in the process makes the code more legible and reduces confusion with all those braces.

You can see how nice the switch block is for yourself in the following examples. First we will use switch

var sweet = "Cake";
switch (sweet) {
    case "Candy":
        console.log("Candy is dandy.");
        break;
    case "Pie":
        console.log("Portal got it wrong. Pie is actually a lie.");
        break;
    default:
        console.log("Cake! My favorite! :D");
        break;
}

… and now for if, else if, and else

var sweet = "Cake";

if (sweet ==="Candy") {
    console.log("Candy is dandy.");
} else if (sweet === "Pie") {
    console.log("Portal got it wrong. Pie is actually a lie.");
} else {
    console.log("Cake! My favorite! :D");
}

See how much nicer that is? It is that typeof simplicity I would like to see in Python’s admittedly already simple syntax.

However, I guess I can’t complain too much. Python does not deal with braces (run from __future__ import braces for yourself to see why), so a layer of complexity is removed, allowing if, elif, and elseblocks to be very small, even if I must set up some conditions.

from __future__ import print_function
sweet = "Cake"
if sweet == "Candy":
    print("Candy is dandy.")
elif sweet == "Pie":
    print("Portal got it wrong. Pie is actually a lie.")
else:
    print("Cake! My favorite! :D")

So much for my wish list. 😛

Advertisements

Triangular Reactions

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s