misleading prefix ++

L

LuciferLeo

given i = 0,
I know i = i + 1 and i += 1 are all correct
but when I type:the interpreter replies:
File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax

so I get the idea that suffix ++ is invalid in python
but when I input:and the interpreter replies
0

Don't you think it is misleading when you expect a variable to
increment?
 
S

Steve Holden

given i = 0,
I know i = i + 1 and i += 1 are all correct
but when I type:


the interpreter replies:
File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax

so I get the idea that suffix ++ is invalid in python
but when I input:


and the interpreter replies
0

Don't you think it is misleading when you expect a variable to
increment?
Terribly. So stop expecting it to increment :)

Seriously, --i is also valid Python. Both expressions apply two unary
operators to a name. Would you have these become illegal, or start to
mean increment/decrement? Either change would break years of backwards
compatibility.

regards
Steve
 
F

Fredrik Lundh

Don't you think it is misleading when you expect a variable to
increment?

no. and in my experience, most people know that they cannot just type
random stuff into a computer and expect it to do what they want.

(have you figured out *why* this is valid syntax, and what it does to
your integer?)

</F>
 
T

Tim Chase

++i
Terribly. So stop expecting it to increment :)

Seriously, --i is also valid Python. Both expressions apply two unary
operators to a name. Would you have these become illegal, or start to
mean increment/decrement? Either change would break years of backwards
compatibility.

It looks like Python behaves more consistantly than C/C++/Java :)
By adding plus/minus signs, you get consistant behavior:

In C-like languages, you get

x: 42
+x: 42
++x: 43
+++x: invalid lvalue in increment -> compile fails
++++x: 45
+++++x: invalid lvalue in increment -> compile fails
++++++x: 48

(actually, g++ accepted this funky syntax, but gcc choked on it,
so linguistic sludge is more tolerable in C++ than in C)

Looks like the OP should be over on c.l.c++ griping about the
inconsistancy of the "unary +" and "unary -" operators ;)

-tkc
(still waiting for my brain to kick in on a Sat. morning...)
 
P

Peter Otten

but when I input:
and the interpreter replies
0

Don't you think it is misleading when you expect a variable to
increment?

You have been warned...

$ cat pp.py
i = 42
++i
print i
--i
$ pychecker pp.py
Processing pp...
42

Warnings...

pp.py:2: Operator (++) doesn't exist, statement has no effect
pp.py:4: Operator (--) doesn't exist, statement has no effect

....or you would have been, had you used the pychecker :)

Now I'm not recommending
.... def __init__(self, value=0):
.... self.value = 0
.... self.half = False
.... def __pos__(self):
.... if self.half:
.... self.value += 1
.... self.half = not self.half
.... return self
.... def __str__(self):
.... return str(self.value)
.... __repr__ = __str__
....2

which is slightly harder to fix than to break but gives some (weak)
motivation not to rule out multiple prefixed signs.

Peter
 
E

Edward Elliott

Peter said:
[snip]
... def __pos__(self):
... if self.half:
... self.value += 1
... self.half = not self.half
... return self [snip]

which leads us to:

Now that is absolutely lovely. Looks like it's time to join the ranks of
Perl and C with an Obfuscated Python Contest. ;)
 
C

Carl Friedrich Bolz

Edward said:
Peter Otten wrote:
[snip]


Now that is absolutely lovely. Looks like it's time to join the ranks of
Perl and C with an Obfuscated Python Contest. ;)

yes, please! and you get special points for programs that seem to do one
thing but do something totally entirely different :).

Cheers,

Carl Friedrich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top