+ 21

Why does, using the '*=' or '/=' operator with the print function result in a syntax error in python?

When trying to understand how these operators work i tried the below #Syntax error v = 20 print(v *= 3.6) #If i break it into two lines it works Fine v = 20 v *= 6 print(v) A minor thing but other languages like Ruby and JavaScript let you assign and print the result in the same line. Similar issue can be observed with '=' operator. #Demo code below https://code.sololearn.com/cVZ2o4ZKAKFG/?ref=app

6th Apr 2019, 2:17 AM
Lord Krishna
Lord Krishna - avatar
28 odpowiedzi
+ 18
So there's a difference between an "expression" and a "statement", and statementy things usually don't produce a value which you can pass around in your code. Think of the line `elif x < 3:` for example. `x *= 10` is not an expression because reasons. You can find similar non-expressions in almost any language. Like, if/else is usually not an expression, so a lot of C-like languages have `?:` to denote if-expressions. It's a bit silly. In ruby, class definitions are expressions: `c = class Foo; end` - that works in almost no language. It's just one of those things. ¯\_(ツ)_/¯ Maybe we switch to LISP, everything is an expression there.
6th Apr 2019, 3:50 AM
Schindlabua
Schindlabua - avatar
+ 13
Just came across Flandre's feed post about inline operations coming to Python (Mainly the Walrus operator := you mentioned). Thought this might interest you. https://www.sololearn.com/post/83109/?ref=app
6th Apr 2019, 5:04 AM
Hatsy Rei
Hatsy Rei - avatar
+ 13
++, -- are also called 'side-effect operators', right? Every day we read in q&a several times about these, how they work, why they produce a certain result in a given example, when and where not to use them (in C(++)). Maybe it's just the attempt to erase all this messiness.
6th Apr 2019, 6:24 AM
HonFu
HonFu - avatar
+ 12
6th Apr 2019, 3:00 AM
Diego
Diego - avatar
+ 10
HonFu If I had to wager, statements were just easier to implement and they didn't give it much thought. Expressions can even be safer than statements; e.g. since if-expressions are guaranteed to return a value you can check that during compile-time in strongly typed languages! Even python got expression-ier over time (`def` got itself a `lambda`, `print` became `print()`), but since there isn't a killer usecase for spending time on turning assingments into expressions it'll just stay that way. In javascript `throw` is in the process of being promoted to an expression because the use cases suddenly emerged after we got arrow functions. At the moment it's not because that's just how it has always been.
6th Apr 2019, 7:42 AM
Schindlabua
Schindlabua - avatar
+ 9
Not being able to do `print(x*=10)` is how python works? Why? Is there a good explanation or reasoning for this behavior? Other languages do not have this as an error and are much more flexible in this area.
6th Apr 2019, 2:51 AM
Lord Krishna
Lord Krishna - avatar
+ 9
Python forbids printing out statements that involve assignment of values.
6th Apr 2019, 8:59 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
That seems to be the case. '*=, /=, += are considered statements like '=', which are forbidden inside expressions — print is one. https://stackoverflow.com/a/2604036 There seems to be a propasal for ':=' which should allow it for assignments, do not know about (*=, +=, +=). If only '=' (specifically it's equivalents +=, *=) were flexible from start this wouldn't have been needed. oh, well ¯\_(ツ)_/¯ Diego Thanks for answering!
6th Apr 2019, 3:26 AM
Lord Krishna
Lord Krishna - avatar
+ 7
I agree with Guido van Rossum😏
7th Apr 2019, 3:35 PM
Mr.Fish
Mr.Fish - avatar
+ 5
Schindlabua, 'there isn't a killer usecase', 'that's just how it has always been'... That's sort of how it feels to me. When I have them available, I use the expression assignments, but when I write Python, I never feel like 'omg, all those extra statements, Python is so verbose, I can't stand it'. Doesn't seem to make such a big difference. Sometimes I miss a do while loop; but the 'crements'... not so much.
6th Apr 2019, 8:39 AM
HonFu
HonFu - avatar
+ 5
Have you read the python last lesson It says that the functions with the following syntax can take the = or any other operator: Def somefun(**args) The print function must be def print(*args)
6th Apr 2019, 8:52 AM
Soham More
Soham More - avatar
+ 5
I'm not sure how that answers the question. Anyway, it's not possible because of a limitation in python. def f(x): return print(x) a= 1 f(a+=3) #syntax error
18th Jun 2019, 3:52 AM
Lord Krishna
Lord Krishna - avatar
+ 4
I don't understand what's the problem here. Its just how python works. You are assigning a value to a variable.
6th Apr 2019, 2:36 AM
Toni Isotalo
Toni Isotalo - avatar
+ 4
You are right. The distinction between statements and expressions is subtle but there which causes these issues. I'm spoiled by languages which let you do just that without any recourse. Two i know picolisp in lisp world, and forth.
6th Apr 2019, 4:05 AM
Lord Krishna
Lord Krishna - avatar
+ 4
I didn't reply to your answer and missed it. Now i do. I'm not sure what's confusing about printing the result of *=, +=, -=,. If they are so much confusing why even have them in the 1st place or try to use them. Losing so much power just to cater to the beginners seems idiotic. No one is a beginner forever. About the example (x=2) i can agree but then it wasn't about that one i had asked. Anyway, i concede it's something which won't work in python. It's fine, i'm not using this lang, even for test purposes.
18th Jun 2019, 4:40 AM
Lord Krishna
Lord Krishna - avatar
+ 4
Which voted answer? They are many, i'm not sure i follow the context. Katie (Ctrl-Alt-Cuteness)
18th Jun 2019, 4:47 AM
Lord Krishna
Lord Krishna - avatar
+ 3
Lord Krishna This is due to the side-effect possibly not being desirable. When I make a one-liner from within bash (invoking Python), I psrsonally use a list if a single variable like so in a loop: >>> import sys; c = [0]; [ None for i in sys.argv[1:] if c.__setitem__(0, c[0] + len(i) ]; print(c[0]) Note that like how I did that, it allows me to do stuff like so: >>> c.__setitem__(0, 5) or c[0] # None or 5 Edit: I figured out why. Python does not allow assignments in place of just any expression.
18th Jun 2019, 4:10 AM
Katie (Ctrl-Alt-Cuteness)
Katie (Ctrl-Alt-Cuteness) - avatar
+ 2
Python has its own way and usage of operations, not following python rules cause errors
6th Apr 2019, 9:27 AM
Hassan Saeedullah
+ 2
It doesn't show any error when you use in C language. But in python means of this operators are may be different.
7th Apr 2019, 10:35 AM
YUGANDHAR SURYA
YUGANDHAR   SURYA - avatar
+ 2
Perhaps readability is one possibility.
7th Apr 2019, 1:50 PM
Michael Williams
Michael Williams - avatar