473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Why is there no post-pre increment operator in python

Anyone has any idea on why is there no post/pre increment operators in
python ?
Although the statement:
++j
works but does nothing

Jan 13 '06 #1
10 17074
ri**************@gmail.com 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".
Jan 13 '06 #2
[ri**************@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 ;-).
Jan 13 '06 #3
ri**************@gmail.com 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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jan 13 '06 #4

ri**************@gmail.com 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 shit happens

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

Jan 13 '06 #5
ri**************@gmail.com 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

Jan 13 '06 #6
In article <11**********************@g43g2000cwa.googlegroups .com>,
"gene tani" <ge*******@gmail.com> 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!
Jan 13 '06 #7

Roy Smith wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
"gene tani" <ge*******@gmail.com> 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

Jan 13 '06 #8
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

Jan 13 '06 #9
"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>

Jan 13 '06 #10

Peter Hansen wrote:
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.)


Sorry, I was trying to be helpful. One thing, i think it helps to
glance over rejected PEPs every once in a while to reinforce what's not
there
http://www.python.org/peps/

-Peter


Gene

Jan 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: william c | last post by:
I don't really know PHP that well. I'm fixing a Perl program that accesses a db after getting form input from a PHP page. If the server-side validation fails I'd like to reload the form with all...
38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
18
by: John Dalberg | last post by:
Hello I have a login form on a page that should post the data to a non Windows server and upon successful authentication, the browser needs to show the secodn url. I did the form post, got...
9
by: David Schofield | last post by:
It seemed too rude to post this in reply to the post which triggered it but I had to get it off my chest. David
3
by: mortb | last post by:
Sometimes you want to transfer to another page and pass some values along not using the query string nor the session. Is there some way to do this? cheers, mortb
6
by: Water Cooler v2 | last post by:
Is there a Forms collection in the .NET framework that contains the instances of the currently loaded (in memory, whether hidden or visible) forms in the current project? I am looking for something...
28
by: ryanhokanson | last post by:
I was just trying to get a list of all my reports along with their widths. I can't use the "Reports" collection because it only works on open reports. So I tried looking into the "AllReports"...
2
by: Jim D | last post by:
Hi Richard. You've helped me out in the past so I thought I'd present you with the latest problem that we can't seem to fix. Consider an ASP page with a form that has a few text fields... name,...
8
by: Mr. SweatyFinger | last post by:
where have they all gone. alt.suicide? alt.findContol.then.slit.your.own.throat?
2
by: Simon Harris | last post by:
Hi All, As you may have seen from my earlier post, I am trying to setup paging on an ASP application, I *think* this is 99% there. The only problem I have left, is when I get to the last page,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.