Python 中的表达式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4782590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:29:34  来源:igfitidea点击:

What is an expression in Python?

python

提问by FloydChen

I have some confusion about its meaning or definition.

我对它的含义或定义有些困惑。

Isn't that some code that produce or calculate new data values? (Says Zelle in his book)

那不是产生或计算新数据值的一些代码吗?(泽尔在他的书中说)

Then I wonder if a string data type is an expression.

然后我想知道字符串数据类型是否是表达式。

If it is, then what does eval()do when its argument is a string?

如果是,那么eval()当它的参数是一个字符串时会做什么?

The book by Zelle sayseval(<string>)evaluates stringas an expression, what does that exactly mean if stringis already an expression?

Zelle 的书说eval(<string>)评估string作为一个表达式,如果string已经是一个表达式,那究竟意味着什么?

And if stringis not an expression, then how come it can occur after print?

如果string不是表达式,那么它怎么会出现在 之后print

采纳答案by Gordon Gustafson

Expressions representsomething, like a number, a string, or an instance of a class. Any value is an expression!

表达式代表一些东西,比如数字、字符串或类的实例。任何值都是一个表达式!

Anything that does somethingis 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.

foo = "hello"是将 foo 赋给表达式“hello”的值的语句。由于代码"hello"是一个简单的表达式,这意味着它不包含任何操作,因此实际上没有计算任何内容,因此 foo 只是分配给"hello"。更复杂的表达式实际上会计算事物,例如添加数字。使用表达式一词似乎使事情变得更加混乱. 表达式只不过是值,除了它们可以进行加法或减法等运算。

evalevaluates the string as if it were a python expression. Eval does 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将字符串作为 Python 表达式求值。Eval 确实将表达式作为参数。但是,这没有什么特别之处,因为每个值都是一个表达式。说“eval 将一个值作为参数”是在说完全相同的事情,但听起来要简单得多。: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.

eval( "2+2" )将字符串传递"2+2"给函数。该函数计算字符串中包含的表达式,结果为 4。

The book by Zelle says eval(<string>)evaluates string as an expression, what does that exactly mean if string is already an expression?

Zelle 的书说将eval(<string>)字符串作为表达式求值,如果字符串已经是表达式,那究竟意味着什么?

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.

任何字符串都是一个表达式,因为它代表一个值。但是,字符串中的内容对于它作为表达式绝对没有影响。如果它是一个值,则它是一个表达式。当它“被 eval 评估为表达式”时,字符串中的字符将被执行,就好像它们是 Python 表达式一样。

回答by kynnysmatto

string is an expression. An expression is anything that has "a value". Like 3, 'Hello world', 1+1, math.sqrt(9), etc. Function names are expressions too.

字符串是一个表达式。表达式是任何具有“值”的东西。像 3、'Hello world'、1+1、math.sqrt(9) 等。函数名也是表达式。

eval() gives you the value of the expression that you give to it as a string. If you say eval('1+1') it returns 2. So it returns the same that would be returned if you simply write: 1+1.

eval() 为您提供作为字符串提供给它的表达式的值。如果你说 eval('1+1') 它返回 2。所以它返回与你简单地写的一样:1+1。

回答by J-P

"Expression" can be a slightly confusing term once you get away from thinking about how Python's own script parser works. The standard documentationmakes a distinction between expressions and "atoms", but I think that makes its terminology pretty restrictive (the BNF diagram at 5.11 implies that to be an expression something mustbe either a lambda form or a conditional expression, I think. My BNF is rusty.)

一旦你不再考虑 Python 自己的脚本解析器是如何工作的,“表达式”可能是一个有点令人困惑的术语。的标准文档使得表达式和“原子”之间的区别,但我认为,使得它的术语漂亮限制性(在5.11的BNF图意味着是一个表达的东西必须是任一个lambda形式或条件表达式,我想。我BNF 生锈了。)

Atoms, on the other hand, seem to cover as @kynnysmatto says 'anything that has "a value"'. Maybe "anything that can be parenthesized, and then from outside the parentheses is indistinguishable from its corresponding value" might be a better definition of an atom.

另一方面,原子似乎涵盖了@kynnysmatto所说的“任何具有“价值”的东西。也许“任何可以被括号括起来,然后从括号之外与其对应的值无法区分的东西”可能是对原子的更好定义。

When Zelle discusses expressions in the context of e.g. eval(foo), I think he implies:

当 Zelle 在例如 eval(foo) 的上下文中讨论表达式时,我认为他暗示:

  • foo is an identifier, which is a type of atom
  • but foo is an identifier ofsomething
  • this "something" is string-like, probably an actual string
  • within the internals of eval(), this string alsoconstitutes a Python "expression" - that is, something Python can parse and make sense of.
  • foo 是一个标识符,它是一种原子类型
  • 但foo是一个标识符东西
  • 这个“东西”是类似字符串的,可能是一个实际的字符串
  • 在 eval() 的内部,这个字符串构成了一个 Python 的“表达式”——也就是说,Python 可以解析和理解的东西。

tl;dr: "expression" as terminology might be best understood in terms of code parsing; when you program yourself, you might find it better to think in terms of "atoms".

tl;dr:“表达式”作为术语可能在代码解析方面得到最好的理解;当你自己编程时,你可能会发现从“原子”的角度思考会更好。

回答by Al Sweigart

TL;DR: Expressions are combinations of values and operators and always evaluate down to a single value. A statement is every other instruction. Some statements contain expressions.

TL;DR:表达式是值和运算符的组合,并且始终计算为单个值。语句是所有其他指令。一些语句包含表达式。

An expressionis an instruction that combines values and operatorsand always evaluates down to a single value.

一个表达式是一个指令联合值和运算符始终计算到一个单一的值

For example, this is an expression:

例如,这是一个表达式:

>>> 2 + 2

The 2s are integer values and the + is the mathematical operator. This expression evaluates down to the single integer value 4.

2 是整数值,+ 是数学运算符。此表达式向下求值为单个整数值 4。

Technically, this is also an expression:

从技术上讲,这也是一个表达式:

>>> 4

As an expression, it evaluates down to the single value 4.

作为表达式,它的计算结果为单个值 4。

When I say values and operators, this isn't limited to math problems:

当我说值和运算符时,这不仅限于数学问题:

>>> 'You will be ' + str(int(myAge) + 1) + ' next year.'

The myAgevariable evaluates to the value inside it. The function call int('5')evaluates to the function's return value, 5. All these string values are combined with the +operator (in this case, it's the string concatenation operator). No matter how big an expression is, it evaluates down to a singlevalue: in this case, the string value 'You will be 6 next year.'

myAge变量的计算结果为其中的值。函数调用的int('5')计算结果为函数的返回值5。所有这些字符串值都与+运算符组合在一起(在本例中,它是字符串连接运算符)。无论表达式有多大,它都会计算为单个值:在这种情况下,字符串值'You will be 6 next year.'

Contrast this with a statement, which is a Python instruction that does not evaluate down to a value. A Python statement is pretty much everything else that isn't an expression. Here's an assignment statement:

将此与statement形成对比,它是一条 Python 指令,不会计算为值。Python 语句几乎就是所有不是表达式的东西。这是一个赋值语句

>>> spam = 2 + 2

Here's an if statement:

这是一个 if 语句:

>>> if spam == 4:

Here's a while statement for an infinite loop:

这是一个无限循环的 while 语句:

>>> while True:

Note that both of these statements contain expressions (even True, which evaluates down to the single value True). But not all statements use expressions in them. Here's a break statement:

请注意,这两个语句都包含表达式(even True,其计算结果为单个值True)。但并非所有语句都在其中使用表达式。这是一个中断语句:

>>> break