経緯
学校の宿題でreadingが出ているので読んでいたらexpressionsとstatementsという概念にぶつかる。読んでもいまいち違いがピンとこないので調べてみることに。学校側もこれだけ読んで全部理解しろと言っているわけではないと信じてる…
def. of statements(文)and expressions(式)
readingによる定義は
Statements typically describe actions. When the Python interpreter executes a statement, it carries out the corresponding action.
ってことはx = 1とかy = "apple"とかを意味してるの?っていうのが自分の解釈でしたが、stackoverflowには
Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator [] and similar, and can be reduced to some kind of "value", which can be any Python object. Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code.
ってことはexpressionsはstatementsとしても捉えられるってことになる。expressionだけで完結させられることもできる一方でstatementは(この辺を読んでると余計そう思わせる)どちらかというと他のブロックと上手く繋がって動くという印象。statementの中にはほぼ確実にexpressionがある。
quoraより
A statement is a complete line of code that performs some action, while an expression is any section of the code that evaluates to a value. Expressions can be combined “horizontally” into larger expressions using operators, while statements can only be combined “vertically” by writing one after another, or with block constructs. Every expression can be used as a statement (whose effect is to evaluate the expression and ignore the resulting value), but most statements cannot be used as expressions.
またこちらのstackoverflowのexpressionに対する解答も分かりやすかった。
Expressions represent something, like a number, a string, or an instance of a class. Any value is an expression!
Anything that does something is a statement. Any assignment to a variable or function call is a statement. Any value
contained in that statement in an expression.
foo = "hello"is a statement that assigns foo to the value of the expression"hello". Since the code"hello"is a simple expression, meaning it contains no operations, nothing is actually evaluated, so foo is just assigned to"hello". More complex expressions actually evaluate things, like adding numbers. Using the word expression seems like it is making things more confusing. Expressions are nothing but values, except they can have operations like addition or subtraction.
evalevaluates the string as if it were a python expression.Evaldoes takes an expression as an argument. However, there's nothing special about this since every single value is an expression. Saying "eval takes a value as an argument" is saying exactly the same thing, but it sounds much simpler. :D
eval( "2+2" )passes the string"2+2"to the function. The function evaluates the expression contained in the string, which comes out to 4.
The book by Zelle says eval() evaluates string as an expression, what does that exactly mean if string is already an expression?
Any string is an expression since it represents a value. However, what is in the string has absolutely no impact on it being an expression. If its a value, its an expression. When it is "evaluated as an expression by eval", the characters inside the string are executed as if they were a python expression.
さらにここでも
文はそれ単独で完結する言語要素です。式はそれ単独では基本的に完結せず、文または式の一部として使用される言語要素です。また、式の最大の特徴として、値を返すという点が挙げられます(文は値を返しません)。例えば、定数式はその値そのものを返します。条件式は真偽値(True/False)を返す式です。条件式の一つとして比較式がありますが、条件式そのものはTrue/Falseを返せばなんでもよいため、単なるBooleanのTrue(定数式)もまた条件式になります。比較式は比較演算子を挟んで左辺と右辺の式を比較し、その結果をTrue/Falseで返すため、条件式になることができます。
ちなみに__evaluate argumentとは簡単に言えばcalling functionを意味する__。関数の括弧の中の引数を実際に引き渡すときに起こる。evaluate functionはcalling functionとは異なる。これはただ単に関数があるよって定義されただけ。なので正しい順番はevaluate function→evaluate argument→function gets calledとなる。
追記
わざわざ探さなくとも次の講義で紹介されていたので一応(笑)
an expression describes a computation and evaluates to a value. all expressions can use function call notation
add ( 2 , 3)というcall expressionはそれぞれ一つ一つがexpressionでもある。addはoperator、()の中に入っているものはoperandと呼ばれる。call expressionはまずoperatorとoperandの認識し、operandをargumentとして扱いfunctionを用いてvalueをだす。ちなみに自明ではあるがこれは2 + 3と表すことも出来る。add(2,3)から2 + 3へと変換することを__syntactic sugar__と呼ぶ。また数学で使われるサイン+, *, - or /を__infix notation__と呼ぶ。
書いてあることは上と全く同じですがこちらにも記載されていたので一部転載
Executing x = 2 does not return a value nor evaluate a function on some arguments, since the purpose of assignment is instead to bind a name to a value. In general, statements are not evaluated but executed; they do not produce a value but instead make some change. Each type of expression or statement has its own evaluation or execution procedure.
追記#2
readingの"Statements" sectionより転載
Expressions can also be executed as statements, in which case they are evaluated, but their value is discarded. Executing a pure function has no effect, but executing a non-pure function can cause effects as a consequence of function application.
ref: pure functionとnon-pure functionの違い
def square(x):
mul(x, x)# Watch out! This call doesn't return a value.
expressionもstatementと同様、処理は行われますが、valueは表示されません。そこでassignment, def, and return statementsなどと合わせてvalueを出します。
追記#3
後の講義で教授がexpressionとstatementについて解説していたのを元に個人的に簡単なダイアグラムを作ってみたので参考まで。
