Python is great, but __

By Erkki Lindpere

I haven’t used Python a lot, but from what I know about it, I consider it one of the good languages. A bonus is that most Python programmers seem pragmatic and humble, which is nice compared to the impression of Ruby community left by some of the more arrogant Ruby folks. But there’s one thing that really irks me about the language, more than the somewhat significant whitespace or some of the syntax quirks:

__init__

What the fuck is up with that? I have seen very few pieces of Python code which didn’t contain those double underscores. How was it decided that commonly used names will contain four underscore characters? I doubt that names such as init, main, iter, etc. were considered so precious that the language must not treat them specially. Perhaps in the early days it wasn’t apparent that the magic methods would be used as often as they are? Or maybe it was but the __ didn’t bother enough people? I would find that strange, but then again people get used to everything. Scala makes magic use of the underscore as well, but I’ve gotten used to the few cases where it’s part of names. Most of the time it’s used as a wildcard, and that doesn’t bother me at all.

Do you program in Python regularly? What do you think about the underscores?

Tags: , , ,

3 Responses to “Python is great, but __”

  1. mattdr Says:

    Yeah. Python has been lauded as “executable pseudocode” — with some justification, too. I have to agree that the double underscores are a pretty ugly syntax wart. if __name__ ==”__main__”? Ugh. I’m also not a fan of explicitly passing “self” into every method. Python is a fine language, but not flawless.

  2. eengbrec Says:

    Names of the form __name__ have the underscores in order to show that they have special meaning. All the operator overloading methods have leading and trailing underscores. Programmers are warned not to make new methods, variables, etc with leading an trailing underscores because in some future release of Python they may just collide with a name with special meaning. The fact that the underscores are slightly annoying helps ensure people heed the warning, even if they aren’t aware of it. Most people don’t like using underscores that way.

    So when you see those underscores in Python, you know it is something with special meaning.

  3. kumar303 Says:

    In addition to what eengbrec said, double underscores also magically create functions that you cannot override or call. In other words, you’d have to type:

    void private final init function() {

    }

    if it were Java. So, in Python, __init__() is private and final but init() or _init() would not be final or private. In general I find private and final methods to be annoying when refactoring or testing but when they are magic hooks into the low level object system (like __init__, __len__, __iter__) then it kinda makes sense.

Leave a Reply

You must be logged in to post a comment.