Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange logic! :)
#1
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
Reply
#2
In example 2, line 3
x = e +=1
rise SyntaxError: Invalid syntax (of course).


What you expect this line to do?
Reply
#3
You are allowed to do:
x = e+1
but not
x = e += 1
because x = e += 1 is same as
x = e = e + 1
Reply
#4
(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
Reply
#5
(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 ?
Reply
#6
(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.
Reply
#7
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
@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.
Reply
#9
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#10
(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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020