Every expression in Lisp has a value, and why is there no return value for this very common expression in Python3?
Reply content:
First of all, because you are using the C + + thinking to understand Python.
No, no, the return value is not the same, and the return value is the result of the function. It should be an expression value, exactly.
The grammar of a language is defined by the human, the assignment is assignment statement, not expression, so there is no value.
In addition, Inside Python function_name (var=????) There is a special semantics, it refers to the argument of the function_name var parameter is????。
For example
deff(x,y): returnstr(x)+str(y)
The above explanations are very clear, summed up is:
The assignment in Python is an assignment, he is not an expression, and it does not return the value of an expression.
One of the benefits of doing this is to make the syntax clearer.
In C, there is a
"In-line assignment"Question, give a chestnut: you intend to determine whether the value of x equals 1000, if equal to 1000 return true, if not equal to return false, the correct code should be:
#include
voidmain(){ intx=200; if(x==1000) printf("true"); else printf("false");}
You have to understand that B=a=1 is a statement, not an expression.
In [1]: def foo(): ...: a = b = 1 ...: In [2]: from dis import disIn [3]: dis(foo) 2 0 LOAD_CONST 1 (1) 3 DUP_TOP 4 STORE_FAST 0 (a) 7 STORE_FAST 1 (b) 10 LOAD_CONST 0 (None) 13 RETURN_VALUE
An assignment statement does not return a value, so it is not an expression because the assignment statement does not require a return value, and Python is neither C nor Lisp.
Even if there is a return value of Lisp, the return value is usually ' (), which tells you not to use the return value to do anything, in fact, and the idea of Python is the same.
The difference is that Python uses a grammatical error to prevent programmers from doing stupid things.
Why does Python assignment not return a value?
The logic of the chained assignment is not the expression to the right of the first evaluation, but the left side, just a syntax. As you can see, the parsing of an assignment statement is obviously not recursive.
Simple statements
assignment_stmt ::= (target_list "=")+ (expression_list | yield_expression)
Why b=a=1 is a reasonable expression in Python3
Chained assignment is just a grammatical structure he's not like CPP. "Assign 1 to A and assign a value to B."
It's assigning 1 to B and a.
and print (a=1) is not
The meaning of this sentence is to assign 1 to print parameter A instead of "Assign 1 to local variable A and then pass the value of a to" as CPP.
A=1 Why there is no return value
A=1 inside the CPP is the expression (expressions) inside the PY called statement (statement)
The statement is like CPP's "return xxx;" No need to have a value
Other than that
the value of an expressionIt's not called a return value. Because the Python language designer believes that the most important function of an assignment is its side effects and should not have a return value like an expression. See: https:// docs.python.org/3/faq/d esign.html?highlight=assignment#why-can-t-i-use-an-assignment-in-an-expression
The assignment statement for
- Python is not a return value, and many languages are not, which is not a rule or convention for the programming language community.
- your
print (a=1) --------------------------------------------------------------------------
in
()
typeerror: ' A ' is an invalid keyword argument for this function
if ( x = 1 ) { #do ....}
-