Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why is there no post-pre increment operator in python

Reply
Thread Tools

Why is there no post-pre increment operator in python

 
 
riteshtijoriwala@gmail.com
Guest
Posts: n/a
 
      01-13-2006
Anyone has any idea on why is there no post/pre increment operators in
python ?
Although the statement:
++j
works but does nothing

 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      01-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Anyone has any idea on why is there no post/pre increment operators in
> python ?


Short answer: Because Guido didn't like them.

Longer answer: Because they encourage people to write cryptic one-liners.
There really isn't anything you can't write with them that you couldn't
write just as well without them. It just takes another line or two of
code. The end result may be a little longer, but it's almost always easier
to understand.

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


Well, it works in the sense that it's not a syntax error, but it doesn't
quite do nothing. It applies the unary + operator to the value of j, then
does it again, then throws away the result. Granted, that's probably not
what you expected, and probably not very useful, but it's not quite
"nothing".
 
Reply With Quote
 
 
 
 
Tim Peters
Guest
Posts: n/a
 
      01-13-2006
[(E-Mail Removed)]
> 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 .
 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      01-13-2006
(E-Mail Removed) writes:
> Anyone has any idea on why is there no post/pre increment operators in
> python ?


For lots of good reasons.

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


So does --j. They both parse as a value with two unary operators
applied to it in succession: +(+(j)) and -(-(j)).

<mike
--
Mike Meyer <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
gene tani
Guest
Posts: n/a
 
      01-13-2006

(E-Mail Removed) wrote:
> Anyone has any idea on why is there no post/pre increment operators in
> python ?
> Although the statement:
> ++j
> works but does nothing


"+=1" and "-=1" inflate your KLOC by .001, but they always work as
expected with integers, it's when you do augmented assignments on lists
and tuples that **** happens

http://zephyrfalcon.org/labs/python_pitfalls.html

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-13-2006
(E-Mail Removed) wrote:
> Anyone has any idea on why is there no post/pre increment operators in
> python ?
> Although the statement:
> ++j
> works but does nothing


The reason is pretty complex, but here it is: Python is not C.

-Peter

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      01-13-2006
In article <(E-Mail Removed) .com>,
"gene tani" <(E-Mail Removed)> wrote:

> http://zephyrfalcon.org/labs/python_pitfalls.html


Thanks for posting that URL; I hadn't seen the list before. Skimming over
it, none of them really seemed noteworthy until I got to "5. Mutable
default arguments", which rather shocked me. Good stuff to know!
 
Reply With Quote
 
gene tani
Guest
Posts: n/a
 
      01-13-2006

Roy Smith wrote:
> In article <(E-Mail Removed) .com>,
> "gene tani" <(E-Mail Removed)> wrote:
>
> > http://zephyrfalcon.org/labs/python_pitfalls.html

>
> Thanks for posting that URL; I hadn't seen the list before. Skimming over
> it, none of them really seemed noteworthy until I got to "5. Mutable
> default arguments", which rather shocked me. Good stuff to know!


here's my full FAQ / gotcha list

http://www.ferg.org/projects/python_gotchas.html
http://zephyrfalcon.org/labs/python_pitfalls.html
http://zephyrfalcon.org/labs/beginners_mistakes.html
http://www.python.org/doc/faq/
http://diveintopython.org/appendix/abstracts.html
http://www.onlamp.com/pub/a/python/2...rn_python.html
http://www.norvig.com/python-iaq.html
http://www.faqts.com/knowledge_base/index.phtml/fid/245
http://amk.ca/python/writing/warts

pls don't hijack threads

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-13-2006
gene tani wrote:
> Roy Smith wrote:
>>Thanks for posting that URL; I hadn't seen the list before.

[...]
>
> pls don't hijack threads


Um, he didn't "hijack" it, he follow a tangent to the discussion and
even changed the Subject line in a very appropriate manner, both of are
completely acceptable netiquette and long-standing Usenet practices.

(Rather like I'm doing here.)

-Peter

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-13-2006
"gene tani" wrote:

> pls don't hijack threads


this is usenet, not gene tani's web board.

if you have trouble dealing with subthreads, get a better news reader.

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM



Advertisments