Python dot-equals (syntax proposal)
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Apr 30 21:16:20 EDT 2010
On Fri, 30 Apr 2010 12:34:34 -0400, D'Arcy J.M. Cain wrote:
> I assume you mean the former to be analagous to "+=" and friends but I
> am not sure since "." isn't an operator.
It's a de facto operator. If you google on "python dot operator" you will
find many people who refer to it as such, and attribute lookup can be
over-ridden at runtime (using __getattribute__, __getattr__, etc.) just
like operators +, -, * etc.
Also you can do this:
>>> s = "something"
>>> s . upper()
'SOMETHING'
>>> (s+" else") . upper()
'SOMETHING ELSE'
And even apply the dot "operator" to floats and ints, although because of
the ambiguity with floats you need to be clever:
>>> 1.2.is_integer()
False
>>> 4 .numerator
4
However, dot is not a "real" operator, whatever that means: internally,
CPython treats it as a delimiter:
http://docs.python.org/reference/lexical_analysis.html#operators
In practice though, I think that's a difference that makes no difference.
It walks like an operator, it swims like an operator, and it quacks like
an operator.
--
Steven
More information about the Python-list
mailing list