Live data from Hacker News

Python and the Principle of Least Astonishment

lucumr.pocoo.org

51–53 of 53 posts

Re: Python and the Principle of Least Astonishment

#51
post #40

Earlier quoted context omitted.

The better explanation is that there are a series of fallback actions for all of the top level functions that delegate to the .__whatever__ methods. For example, if you write a class that quacks like a collection that has a length (it implements .__len__) Python can use that to get a value for bool() even if you didn't implement .__nonzero__.

When people inquire about the len() function it is mainly because it is (or at least looks like) a global function rather than an object method. In short, people wonder why you have to call len(x) instead of x.len(). This is a valid concern seeing that Python's standard library is, by and large, object oriented (c.f. string methods like "upper" or list methods like "append" or file methods like "read"). Neither the a…

Guido on the subject: http://mail.python.org/pipermail/python-3000/2006-November/0...

tldr - He thought it more intuitive.

Re: Python and the Principle of Least Astonishment

#52

Earlier quoted context omitted.

When people inquire about the len() function it is mainly because it is (or at least looks like) a global function rather than an object method. In short, people wonder why you have to call len(x) instead of x.len(). This is a valid concern seeing that Python's standard library is, by and large, object oriented (c.f. string methods like "upper" or list methods like "append" or file methods like "read"). Neither the a…

Guido on the subject: http://mail.python.org/pipermail/python-3000/2006-November/0... tldr - He thought it more intuitive.

Very interesting, thanks for the link! I don't feel like arguing with the father of the language and this story is already on its way out of the HN front page so I'll leave it at that. If anything it's comforting to know that such a core language feature didn't come about by accident.

Re: Python and the Principle of Least Astonishment

#53
post #33

Earlier quoted context omitted.

Would you mean this to be a namespace which only the Python implementation could access? For example, if the protocol "it:next" is added (which is equivalent to next(it)) then is there any way to support that syntax in an older version? With next() as a builtin it's easy; try: next except NameError, and in case of exception, implement the function yourself. But if I can't implement it myself then there's no good migr…

"is there any way to support that syntax in an older version" Nop, that is exactly what I meant with decisions haunting you forever. You better start a new language than radically change the current implementation. With regard to my particular choice of ':' there are three solutions: 1. pick any of the 100k unicode chars for internal methods but imperative is to differentiate them to freely use attributes and methods…

My point is that it isn't "as simple as using another char." #1 doesn't work if you want things visible on most keyboards in the world. #2 is counter to a design principle (which Python uses) that humans are less good at resolving ambiguity than people (see "template >" in C++ pre 2011), and #3 as a specific case means problems getting methods of floats, as in "1..hex()". Of course Python has that already with "1 .bit_length()" being different than "1.bit_length()".

I agree that seemingly minor decisions can have a big impact in the future of a language. What I disagree with is that this choice of a special syntax for built-in methods is "simple", and I believe that doing it yields a language not meant for stardom.

Post reply on HN