[Python-Dev] ++x oddness

M.-A. Lemburg mal@lemburg.com
Sun, 12 Aug 2001 12:47:38 +0200


Paul Prescod wrote:
> 
> "M.-A. Lemburg" wrote:
> >
> >..
> >
> > Why ? ++x can be put to some real use: I have a counter
> > type which actually uses ++x to increment the counter.
> 
> Do you implement a matching "x++"? What's wrong with x.inc()?

x++ does not work. x.inc() would work too, but it involves a
method lookup and function call, which "++x" doesn't.
 
> > Note that the interpreter sees ++x as +(+x), that is the "+" is
> > interpreted as unary + -- perfectly legal Python if you ask me.
> 
> Legal but perverse. Your users will expect +++x and x++ to be equally
> valid and have their C semantics. Plus you are using a Python construct
> that means one thing to mean something totally different just because it
> means the totally different thing in other languages. In Python that
> syntax would usually be side-effect free (and useless).

Well, it is not necessarily side-effect free, since Python does call
a method slot on the underlying object and that slot can change
of course change state.

Nevermind, the syntax is only an experiment -- I just wanted to
point out that "++x" is legal in Python (you can place any number
of + before the object, BTW).

Some examples:

>>> +++++1
1
>>> ------2
2
>>> --2
2
>>> -2
-2
>>> ---2
-2
>>> ----2
2

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/