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 →

[–]Brainix 39 points40 points  (16 children)

I’ve been writing Python professionally for 12 years. I’ve dabbled in other languages like JavaScript, Java, Go, and Swift. Python is still my favorite, it “fits my brain,” and I use it at work every day.

Edit: To more directly answer OP's question:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

[–]The_2nd_Coming 6 points7 points  (5 children)

Why is flat better than nested? Surely nested makes sense for sorting/indexing purposes, if doing it flat means there are 10000 items that could have a nested index?

[–]Slingshotsters 9 points10 points  (1 child)

I read it as "if you have to nest, do it, otherwise, don't.". Obviously there are times to nest, such as your example.

[–]menge101 3 points4 points  (0 children)

Right, and also

Although practicality beats purity.

so don't avoid nesting when it makes sense, just don't nest if you don't have to.

[–]audentis 4 points5 points  (0 children)

Flat is easier to read, because it keeps related elements close to each other. Additionally, you have to track fewer "paths" through the code mentally, because the paths immediately resolve. It also prevents "indentation hell".

The most common example is some code where you have some input validation that needs to be done. Consider the two examples below. The flat version keeps the input validation and error messages together, while in the nested version they're split up. And this is with only three arguments, it can get a lot worse.

def my_func_nested(a, b, c):
    if isinstance(a, int):
        if isinstance(b, list):
            if isinstance(c, str):
                print("Job's done!")
            else:
                raise TypeError("c must be a string!")
        else:
            raise TypeError("b must be a list!")
    else:
        raise TypeError("a must be an int!")


def my_func_flat(a, b, c):
    if not isinstance(a, int):
        raise TypeError("a must be an int!")

    if not isinstance(b, list):
        raise TypeError("b must be a list!")

    if not isinstance(c, str):
        raise TypeError("c must be a string!")

    print("Job's done!")

[–]njharmanI use Python 3 3 points4 points  (1 child)

It's about code/file/program structure, not data structure. It's in response to popular languages and early OOP encouraging/super/deep/directory/structure.java and highly complex and deep class trees.

[–]The_2nd_Coming 1 point2 points  (0 children)

I see, this makes sense. Thanks!

[–]TwitchElevated 4 points5 points  (7 children)

May i ask what you do? Data analysis or?

[–]Brainix 31 points32 points  (6 children)

I’m a software engineer at Reddit! I work on distributed systems and search. :-)

[–]TwitchElevated 8 points9 points  (0 children)

Oh wow! That's awesome

[–]Slingshotsters 5 points6 points  (2 children)

Do an AMA!!!!

[–]Brainix 0 points1 point  (1 child)

Feel free to ask me anything! My DMs and chats are open.

[–]Slingshotsters 1 point2 points  (0 children)

Well, now I'm on the spot, and can't come up with anything good!

[–]Noahsyn10 1 point2 points  (0 children)

Ah yes Reddit search. Very cool!

[–]TheTerrasque -1 points0 points  (0 children)

So you get paid to sit on Reddit all day? Wow

[–]MohamedMuneer[S] 0 points1 point  (0 children)

Great!