Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

  Better than ugly, beautiful is.
  Better then implicit, explicit is.
  Better than complex, simple is.
  Better than complicated, complex is.
  Better than nested, flat is.
  Better than dense, sparse is.
  Counts, readability does.
  Special enough to break the rules, special cases are not.
  But beaten by practicality, purity is.
  Silently passed, an error should never be.
  Unless explicitly, is it silenced.
  In the face of ambiguity, the temptation to guess, refuse you must.
  One, preferably only one, way to do it, there should be.
  Not obvious, it might be. 
  Better than never, is now.
  But often better than right now, is never.
  If hard to explain, bad it is.
  If easy to explain, good it may be.
  Namespaces are a honking good idea - more of them we should do!
-- The Zen of Python, Yoda


> Namespaces are a honking good idea - we should do more of them!

That should be:

   english.namespaces english.verbs.are english.articles.a american.english.vernacular.adjective.honking ...


No, that's Java. Flat is better than nested: no self-respecting Python programmer would write hierarchy that deep.


No, Java looks like the famous quotation about the nail and the horse from:

http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...


So which rule take precedents here? Namespace is obviously not flat, at least it's more nested than one without namespace.

That's the problem I have with people praising Zen of Python as if it means anything. It's like a bible you just pick the verse you like to justify your action even if it might conflict with other rules. Then you praise the whole thing for being so wise.


It means a lot: good python code follows it. However, yes, some of the verses conflict, because it turns out that good advice sometimes contradicts itself. All I can say is don't go too far in either direction.


Much of the point of the Zen of Python is that it's self-contradictory.


Balanced, you must be.


You would prefer PHP[1] where the standard library has no namespaces?

[1] I refer to "classic" PHP. No clue if anything PHP5+ fixed this, though I doubt that they would make such a breaking changed even across major revisions.


Yes, I would. And though PHP is widely agreed to be piece of shit (it seems: I don't work with PHP so I'm here only relaying this popular sentiment), that doesn't tarnish the idea by association (which is what I sense you might be trying to do).

ISO C and POSIX also have a flat library namespace, together with the programs written on top. Yet, people write big applications and everything is cool. Another example is that every darned non-static file-scope identifier in the Linux kernel that isn't in a module is in the same global namespace.

Namespaces are uglifying and an idiotic solution in search of a problem. They amount to run-time cutting and pasting (one of the things which the article author is against). Because if you have some foo.bar.baz, often things are configured in the program so that just the short name baz is used in a given scope. So effectively the language is gluing together "foo.bar" and "baz" to resolve the unqualified reference. The result is that when you see "baz" in the code, you don't know which "baz" in what namespace that is.

The ISO C + POSIX solution is far better: read, fread, aio_read, open, fopen, sem_open, ...

You never set up a scope where "sem_" is implicit so that "open" means "sem_open".

Just use "sem_open" when you want "sem_open". Then I can put the cursor on it and get a man page in one keystroke.

Keep the prefixes short and sweet and everything is cool.

I was a big believer in namespaces 20 years ago when they started to be used in C++. I believed the spiel about it providing isolation for large scale projects. I don't believe it that much any more, because projects in un-namespaced C have gotten a lot larger since then, and the sky did not fall.

Scoping is the real solution. Componentize the software. Keep the component-private identifiers completely private. (For instance, if you're making shared libs, don't export any non-API symbols for dynamic linking at all.) Expose only API's with a well-considered naming scheme that is unlikely to clash with anything.


PHP namespacing in many ways ruined the language. I'm not just talking about the poorly chosen use of the backslash '\' path separator or the fact that namespaces aren't automatically inferred which causes me to have to write "use" endless times at the top of the file which destroys my productivity when working outside an IDE.

I'm talking about the heart of PHP which is stream processing. Why in the world would you destroy the notion of simply including other source files in order to wedge in this C++ centric notion of a namespace? Before "namespace" and "use", the idea was that all of the files included together can be treated as one large file, and sadly that conceptual simplicity has been lost.

Also the lost opportunity of having objects be associative arrays like Javascript, combined with namespacing, have convinced me that perhaps PHP should be forked to a language more in-line with its roots. I haven't tried Hack or PHP7 yet but I am apprehensive that they probably make things worse in their own ways.

I think of PHP as a not-just-write-only version of Perl, lacking the learning curve of Ruby, with far surpassed forgiveness and access to system APIs over Javascript/NodeJS. Which is why it's still my favorite language, even though the curators have been asleep at the wheel at the most basic levels.


The standard library isn't namespaced. If you want to use a stdlib function inside an NS, you can without issue. If you want to use a stdlib class, or any top level class, it needs backslash before its name or you need to import it.

There is a community move towards a standard namespacing with PHP-FIG. This is useful and we are seeing lots of progress on internals thanks to the work by the community.

Like a lot of things, PHP name spaces are or were a big mess. Lots of progress is being made to improve though I think a lot must remain.

I've thought about creating a project that packages up various categories in the stdlib into namespaces with consistent inputs and outputs. That would be nice but the process isn't a lot of fun.


(an attempt to translate to standard english grammar, for the benefit of other non-native English readers, who may also struggle to parse this)

  Beautiful is better than ugly.
  Explicit is better then 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 are not special enough to break the rules.
  But purity is beaten by practicality.
  An error should never be silently passed.
  Unless it is silenced explicitly.
  You must refuse the temptation to guess in the face of ambiguity.
  There should be one, preferably only one, way to do it.
  It might not be obvious. 
  Now is better than never.
  But never is often better than right now.
  It is bad if it is hard to explain.
  It may be good if it is easy to explain.
  Namespaces are a honking good idea - we should do more of them!


instead of offering your own translation, we can instead go back to the original english. Run 'python -m this' and you will see

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!


Now those are some best practices I can live by.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: