Double underscores -- ugly?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Feb 20 07:25:33 EST 2008


On Tue, 19 Feb 2008 21:50:33 -0800, Ivan Illarionov wrote:

> I would like to see something like %init or &init to be converted to
> __init__ behind the scenes.

Unfortunately -- or perhaps fortunately -- % clashes with the already 
established uses of % as the modulus operator for numbers and the 
interpolation operator for strings.

>>> 5 % 3
2
>>> "hello %s" % "world"
'hello world'


The last thing I want is to have to study code carefully to try to work 
out whether %name means __name__ or %name.

Similarly for &, the bitwise and operator:

>>> 9 & 12
8



> And $something to be converted to self.something.

Well, at least $ is unused in Python at the moment. That's a point in the 
idea's favour. It's a tiny point, but a point.


> But, unfortunately, most Python people would consider
> this ugly 

Yes.

> just because Perl uses too much syntactic sugar 

No. It's because Perl uses too much *line noise*, punctuation characters 
instead of syntax.


> and anything
> Perl-like is considered ugly in Python community. So, unless Perl die, I
> believe that Python will remain sugar-free.

Python has lots of syntactic sugar. Some examples:

"class Foo" is syntactic sugar for a call to the new.classobj() function.

Both "def foo():" and "lambda :" are syntactic sugar for a call to the 
new.function() function.

Double-underscore method names are syntactic sugar.

Decorators are syntactic sugar.

Generator expressions and list comprehensions are syntactic sugar.

Dict, tuple and list literals {a: b} (a, b) [a, b] are syntactic sugar.

Method calls and method definitions are syntactic sugar.

The trinary if operator "x if flag else y" is syntactic sugar.

Most of these have been part of Python since the earliest days. Some of 
them, like method calls, are so established in Python and other languages 
that it's hard to remember that they could be done another way, without 
syntax support, but of course they could be:

# instance.method(arg)
getattr(instance, 'method')(arg)


Python uses relatively little punctuation. That's one of the strengths of 
the language, and why its very unlikely that your suggestions will be 
implemented in Python.



-- 
Steven



More information about the Python-list mailing list