Why is there no post-pre increment operator in python

Tim Peters tim.peters at gmail.com
Thu Jan 12 22:57:10 EST 2006


[riteshtijoriwala at gmail.com]
> Anyone has any idea on why is there no post/pre increment operators in
> python ?

Maybe because Python doesn't aim at being a cryptic portable assembly
language?  That's my guess ;-)

> Although the statement:
> ++j
> works but does nothing

That depends on the type of j, and how it implements the __pos__()
method.  The builtin numeric types (integers, floats, complex)
implement __pos__ to return the base-class part of `self`.  That's not
the same as doing nothing.  There is no "++" operator in Python, BTW
-- that's two applications of the unary-plus operator.

>>> class MyFloat(float):
...     pass
>>> x = MyFloat(3.5)
>>> x
3.5
>>> type(x)
<class '__main__.MyFloat'>
>>> type(+x)  # "downcasts" to base `float` type
<type 'float'>
>>> type(x.__pos__())   # same thing, but wordier
<type 'float'>

If you want, you can implement __pos__ in your class so that

   +a_riteshtijoriwala_object

posts messages to comp.lang.c asking why C is so inflexible ;-).



More information about the Python-list mailing list