Python Forum
Strange logic! :) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Strange logic! :) (/thread-3178.html)

Pages: 1 2


Strange logic! :) - alsimm - May-03-2017

Hi all
python 2.7
can someone explain the logic here I am confused Confused (doesn't take much to confuse me Smile
In the first example everything works OK.But the second example despite the fact that x is a variable
it doesn't work.Now the question here is this...does python have strange logic...or do I Wink
example 1
a = 1
print a
a +=1
print a
example 2

e = 10
print e
x = e +=1
print x
kind regards
al


RE: Strange logic! :) - buran - May-03-2017

In example 2, line 3
x = e +=1
rise SyntaxError: Invalid syntax (of course).


What you expect this line to do?


RE: Strange logic! :) - idontreallywolf - May-03-2017

You are allowed to do:
x = e+1
but not
x = e += 1
because x = e += 1 is same as
x = e = e + 1


RE: Strange logic! :) - buran - May-03-2017

(May-03-2017, 10:34 AM)idontreallywolf Wrote: because x = e += 1 is same as
x = e = e + 1


actually, the first one is invalid syntax and rise SynatxError,
while second one is perfectly fine

e = 10
x = e = e + 1
print x
print e
Output:
11 11



RE: Strange logic! :) - idontreallywolf - May-05-2017

(May-03-2017, 10:39 AM)buran Wrote:
(May-03-2017, 10:34 AM)idontreallywolf Wrote: because x = e += 1 is same as
x = e = e + 1


actually, the first one is invalid syntax and rise SynatxError,
while second one is perfectly fine

e = 10
x = e = e + 1
print x
print e
Output:
11 11

Then why would x = e += 1 be invalid if its the same as x = e = e + 1 ?


RE: Strange logic! :) - buran - May-05-2017

(May-05-2017, 06:44 AM)idontreallywolf Wrote: Then why would x = e += 1 be invalid if its the same as x = e = e + 1 ?

You are the one that repeatedly claims they are the same, not me.... :-)

x = e = e + 1 is called chained assignment and is valid python code.


RE: Strange logic! :) - volcano63 - May-05-2017

(May-05-2017, 06:53 AM)buran Wrote:
(May-05-2017, 06:44 AM)idontreallywolf Wrote: Then why would x = e += 1 be invalid if its the same as x = e = e + 1 ?

You are the one that repeatedly claims they are the same, not me.... :-)

x = e = e + 1 is called chained assignment and is valid python code.

Assignment in Python - unlike in C - does not return value - that why in Python if x=3 is illegal too, and Python programmers don't have to write
if 3 == x
in
x = e += 1
right operand is assignment - unlike in the example above, where right operand is e + 1


RE: Strange logic! :) - buran - May-05-2017

@volcano63, I don't understand what you mean that Python programmers don't have to write if 3 == x and once again x = e += 1 is invalid, raising SyntaxError.


RE: Strange logic! :) - volcano63 - May-05-2017

(May-05-2017, 07:22 AM)buran Wrote: @volcano63, I don't understand what you mean that Python programmers don't have to write if 3 == x
Many C/C++ programmers acquire the habit to write if (3 == x) instead of if (x == 3), because if you accidentally omit =, the code will still compile - but the meaning will change.

if (3 = x) is illegal assignment, failing compilation.
(May-05-2017, 07:22 AM)buran Wrote: and once again x = e += 1 is invalid, raising SyntaxError.
I thought I was enhancing your point, not arguing it.


RE: Strange logic! :) - nilamo - May-19-2017

(May-05-2017, 01:03 PM)volcano63 Wrote: if (3 = x) is illegal assignment, failing compilation.
I do this all the time, because I like when the compiler tells me I'm stupid. I believe it's commonly referred to as yoda checking, because it sounds backwards. (https://en.wikipedia.org/wiki/Yoda_conditions)