标签归档:expression

Python中Lambda表达式内的赋值

问题:Python中Lambda表达式内的赋值

我有一个对象列表,我想使用filterlambda表达式删除除一个以外的所有空对象。

例如,如果输入是:

[Object(name=""), Object(name="fake_name"), Object(name="")]

…那么输出应为:

[Object(name=""), Object(name="fake_name")]

有没有一种方法可以向lambda表达式添加赋值?例如:

flag = True 
input = [Object(name=""), Object(name="fake_name"), Object(name="")] 
output = filter(
    (lambda o: [flag or bool(o.name), flag = flag and bool(o.name)][0]),
    input
)

I have a list of objects and I want to remove all objects that are empty except for one, using filter and a lambda expression.

For example if the input is:

[Object(name=""), Object(name="fake_name"), Object(name="")]

…then the output should be:

[Object(name=""), Object(name="fake_name")]

Is there a way to add an assignment to a lambda expression? For example:

flag = True 
input = [Object(name=""), Object(name="fake_name"), Object(name="")] 
output = filter(
    (lambda o: [flag or bool(o.name), flag = flag and bool(o.name)][0]),
    input
)

回答 0

:=Python 3.8中添加的赋值表达式运算符支持lambda表达式内部的赋值。出于语法原因,此运算符只能出现在括号(...),括号[...]或括号中{...}。例如,我们将能够编写以下内容:

import sys
say_hello = lambda: (
    message := "Hello world",
    sys.stdout.write(message + "\n")
)[-1]
say_hello()

在Python 2中,可以执行本地分配作为列表理解的副作用。

import sys
say_hello = lambda: (
    [None for message in ["Hello world"]],
    sys.stdout.write(message + "\n")
)[-1]
say_hello()

但是,由于您的变量flag位于外部作用域而不是的作用域中,因此在您的示例中无法使用这两个方法lambda。这与无关lambda,这是Python 2中的常规行为。Python3可让您nonlocaldefs 内使用关键字解决此问题,但nonlocal不能在lambdas 内使用。

有一种解决方法(请参阅下文),但是当我们讨论该主题时…


在某些情况下,您可以使用它来执行a内部的所有操作lambda

(lambda: [
    ['def'
        for sys in [__import__('sys')]
        for math in [__import__('math')]

        for sub in [lambda *vals: None]
        for fun in [lambda *vals: vals[-1]]

        for echo in [lambda *vals: sub(
            sys.stdout.write(u" ".join(map(unicode, vals)) + u"\n"))]

        for Cylinder in [type('Cylinder', (object,), dict(
            __init__ = lambda self, radius, height: sub(
                setattr(self, 'radius', radius),
                setattr(self, 'height', height)),

            volume = property(lambda self: fun(
                ['def' for top_area in [math.pi * self.radius ** 2]],

                self.height * top_area))))]

        for main in [lambda: sub(
            ['loop' for factor in [1, 2, 3] if sub(
                ['def'
                    for my_radius, my_height in [[10 * factor, 20 * factor]]
                    for my_cylinder in [Cylinder(my_radius, my_height)]],

                echo(u"A cylinder with a radius of %.1fcm and a height "
                     u"of %.1fcm has a volume of %.1fcm³."
                     % (my_radius, my_height, my_cylinder.volume)))])]],

    main()])()

半径为10.0cm,高度为20.0cm的圆柱体的容积为6283.2cm³。
半径为20.0cm,高度为40.0cm的圆柱体的容积为50265.5cm³。
半径为30.0cm,高度为60.0cm的圆柱体的体积为169646.0cm³。

请不要。


…回到您的原始示例:尽管您无法flag在外部范围内对变量进行赋值,但可以使用函数来修改先前分配的值。

例如,flag可以是.value我们使用设置的对象setattr

flag = Object(value=True)
input = [Object(name=''), Object(name='fake_name'), Object(name='')] 
output = filter(lambda o: [
    flag.value or bool(o.name),
    setattr(flag, 'value', flag.value and bool(o.name))
][0], input)
[Object(name=''), Object(name='fake_name')]

如果我们想适合上述主题,可以使用列表推导代替setattr

    [None for flag.value in [bool(o.name)]]

但是实际上,在严肃的代码中,lambda如果要进行外部分配,则应始终使用常规函数定义而不是a 。

flag = Object(value=True)
def not_empty_except_first(o):
    result = flag.value or bool(o.name)
    flag.value = flag.value and bool(o.name)
    return result
input = [Object(name=""), Object(name="fake_name"), Object(name="")] 
output = filter(not_empty_except_first, input)

The assignment expression operator := added in Python 3.8 supports assignment inside of lambda expressions. This operator can only appear within a parenthesized (...), bracketed [...], or braced {...} expression for syntactic reasons. For example, we will be able to write the following:

import sys
say_hello = lambda: (
    message := "Hello world",
    sys.stdout.write(message + "\n")
)[-1]
say_hello()

In Python 2, it was possible to perform local assignments as a side effect of list comprehensions.

import sys
say_hello = lambda: (
    [None for message in ["Hello world"]],
    sys.stdout.write(message + "\n")
)[-1]
say_hello()

However, it’s not possible to use either of these in your example because your variable flag is in an outer scope, not the lambda‘s scope. This doesn’t have to do with lambda, it’s the general behaviour in Python 2. Python 3 lets you get around this with the nonlocal keyword inside of defs, but nonlocal can’t be used inside lambdas.

There’s a workaround (see below), but while we’re on the topic…


In some cases you can use this to do everything inside of a lambda:

(lambda: [
    ['def'
        for sys in [__import__('sys')]
        for math in [__import__('math')]

        for sub in [lambda *vals: None]
        for fun in [lambda *vals: vals[-1]]

        for echo in [lambda *vals: sub(
            sys.stdout.write(u" ".join(map(unicode, vals)) + u"\n"))]

        for Cylinder in [type('Cylinder', (object,), dict(
            __init__ = lambda self, radius, height: sub(
                setattr(self, 'radius', radius),
                setattr(self, 'height', height)),

            volume = property(lambda self: fun(
                ['def' for top_area in [math.pi * self.radius ** 2]],

                self.height * top_area))))]

        for main in [lambda: sub(
            ['loop' for factor in [1, 2, 3] if sub(
                ['def'
                    for my_radius, my_height in [[10 * factor, 20 * factor]]
                    for my_cylinder in [Cylinder(my_radius, my_height)]],

                echo(u"A cylinder with a radius of %.1fcm and a height "
                     u"of %.1fcm has a volume of %.1fcm³."
                     % (my_radius, my_height, my_cylinder.volume)))])]],

    main()])()

A cylinder with a radius of 10.0cm and a height of 20.0cm has a volume of 6283.2cm³.
A cylinder with a radius of 20.0cm and a height of 40.0cm has a volume of 50265.5cm³.
A cylinder with a radius of 30.0cm and a height of 60.0cm has a volume of 169646.0cm³.

Please don’t.


…back to your original example: though you can’t perform assignments to the flag variable in the outer scope, you can use functions to modify the previously-assigned value.

For example, flag could be an object whose .value we set using setattr:

flag = Object(value=True)
input = [Object(name=''), Object(name='fake_name'), Object(name='')] 
output = filter(lambda o: [
    flag.value or bool(o.name),
    setattr(flag, 'value', flag.value and bool(o.name))
][0], input)
[Object(name=''), Object(name='fake_name')]

If we wanted to fit the above theme, we could use a list comprehension instead of setattr:

    [None for flag.value in [bool(o.name)]]

But really, in serious code you should always use a regular function definition instead of a lambda if you’re going to be doing outer assignment.

flag = Object(value=True)
def not_empty_except_first(o):
    result = flag.value or bool(o.name)
    flag.value = flag.value and bool(o.name)
    return result
input = [Object(name=""), Object(name="fake_name"), Object(name="")] 
output = filter(not_empty_except_first, input)

回答 1

您不能真正维护filter/ lambda表达式中的状态(除非滥用全局命名空间)。但是,您可以使用在reduce()表达式中传递的累加结果来实现类似的目的:

>>> f = lambda a, b: (a.append(b) or a) if (b not in a) else a
>>> input = ["foo", u"", "bar", "", "", "x"]
>>> reduce(f, input, [])
['foo', u'', 'bar', 'x']
>>> 

当然,您可以稍微调整一下条件。在这种情况下,它会过滤出重复项,但是您也可以使用a.count(""),例如,仅限制空字符串。

不用说,您可以执行此操作,但实际上不应该这样做。:)

最后,您可以在纯Python中执行任何操作lambdahttp : //vanderwijk.info/blog/pure-lambda-calculus-python/

You cannot really maintain state in a filter/lambda expression (unless abusing the global namespace). You can however achieve something similar using the accumulated result being passed around in a reduce() expression:

>>> f = lambda a, b: (a.append(b) or a) if (b not in a) else a
>>> input = ["foo", u"", "bar", "", "", "x"]
>>> reduce(f, input, [])
['foo', u'', 'bar', 'x']
>>> 

You can, of course, tweak the condition a bit. In this case it filters out duplicates, but you can also use a.count(""), for example, to only restrict empty strings.

Needless to say, you can do this but you really shouldn’t. :)

Lastly, you can do anything in pure Python lambda: http://vanderwijk.info/blog/pure-lambda-calculus-python/


回答 2

当您可以删除所有空值并在输入大小更改时放回一个值时,就无需使用lambda :

input = [Object(name=""), Object(name="fake_name"), Object(name="")] 
output = [x for x in input if x.name]
if(len(input) != len(output)):
    output.append(Object(name=""))

There’s no need to use a lambda, when you can remove all the null ones, and put one back if the input size changes:

input = [Object(name=""), Object(name="fake_name"), Object(name="")] 
output = [x for x in input if x.name]
if(len(input) != len(output)):
    output.append(Object(name=""))

回答 3

尽管可以与和朋友一起执行各种技巧,但表达式=内部不可能进行普通赋值()。 lambdasetattr

但是,解决您的问题实际上非常简单:

input = [Object(name=""), Object(name="fake_name"), Object(name="")]
output = filter(
    lambda o, _seen=set():
        not (not o and o in _seen or _seen.add(o)),
    input
    )

这会给你

[Object(Object(name=''), name='fake_name')]

如您所见,它将保留第一个空白实例,而不是最后一个。如果您需要最后一个,则将进入filter的列表反向,然后反转来自的列表filter

output = filter(
    lambda o, _seen=set():
        not (not o and o in _seen or _seen.add(o)),
    input[::-1]
    )[::-1]

这会给你

[Object(name='fake_name'), Object(name='')]

有一点要注意的:为了让这个与任意对象的工作,这些对象必须正确贯彻__eq____hash__作为解释在这里

Normal assignment (=) is not possible inside a lambda expression, although it is possible to perform various tricks with setattr and friends.

Solving your problem, however, is actually quite simple:

input = [Object(name=""), Object(name="fake_name"), Object(name="")]
output = filter(
    lambda o, _seen=set():
        not (not o and o in _seen or _seen.add(o)),
    input
    )

which will give you

[Object(Object(name=''), name='fake_name')]

As you can see, it’s keeping the first blank instance instead of the last. If you need the last instead, reverse the list going in to filter, and reverse the list coming out of filter:

output = filter(
    lambda o, _seen=set():
        not (not o and o in _seen or _seen.add(o)),
    input[::-1]
    )[::-1]

which will give you

[Object(name='fake_name'), Object(name='')]

One thing to be aware of: in order for this to work with arbitrary objects, those objects must properly implement __eq__ and __hash__ as explained here.


回答 4

更新

[o for d in [{}] for o in lst if o.name != "" or d.setdefault("", o) == o]

或使用filterlambda

flag = {}
filter(lambda o: bool(o.name) or flag.setdefault("", o) == o, lst)

上一个答案

好的,您是否坚持使用filter和lambda?

似乎通过字典理解会更好,

{o.name : o for o in input}.values()

我认为Python不允许在lambda中进行赋值的原因与为什么它在理解中不允许进行赋值的原因相似,这与以下事实有关:这些东西都是在C一边评估的,因此可以给我们一个增加速度。至少那是看完Guido的一篇文章后给我的印象。

我的猜测是,这也将违背其哲学一个在Python做任何一件事情的正确方法。

UPDATE:

[o for d in [{}] for o in lst if o.name != "" or d.setdefault("", o) == o]

or using filter and lambda:

flag = {}
filter(lambda o: bool(o.name) or flag.setdefault("", o) == o, lst)

Previous Answer

OK, are you stuck on using filter and lambda?

It seems like this would be better served with a dictionary comprehension,

{o.name : o for o in input}.values()

I think the reason that Python doesn’t allow assignment in a lambda is similar to why it doesn’t allow assignment in a comprehension and that’s got something to do with the fact that these things are evaluated on the C side and thus can give us an increase in speed. At least that’s my impression after reading one of Guido’s essays.

My guess is this would also go against the philosophy of having one right way of doing any one thing in Python.


回答 5

TL; DR:使用功能惯用法时,最好编写功能代码

正如许多人指出的那样,在Python中不允许进行lambda赋值。通常,在使用功能性习惯用法时,您最好以功能性方式进行思考,这意味着在没有副作用的情况下也没有赋值。

这是使用lambda的功能解决方案。fn为了清楚起见,我将lambda分配给了它(并且因为它有点长)。

from operator import add
from itertools import ifilter, ifilterfalse
fn = lambda l, pred: add(list(ifilter(pred, iter(l))), [ifilterfalse(pred, iter(l)).next()])
objs = [Object(name=""), Object(name="fake_name"), Object(name="")]
fn(objs, lambda o: o.name != '')

您也可以通过稍微改变一些东西来处理迭代器而不是列表。您也有一些不同的进口。

from itertools import chain, islice, ifilter, ifilterfalse
fn = lambda l, pred: chain(ifilter(pred, iter(l)), islice(ifilterfalse(pred, iter(l)), 1))

您始终可以重新组织代码以减少语句的长度。

TL;DR: When using functional idioms it’s better to write functional code

As many people have pointed out, in Python lambdas assignment is not allowed. In general when using functional idioms your better off thinking in a functional manner which means wherever possible no side effects and no assignments.

Here is functional solution which uses a lambda. I’ve assigned the lambda to fn for clarity (and because it got a little long-ish).

from operator import add
from itertools import ifilter, ifilterfalse
fn = lambda l, pred: add(list(ifilter(pred, iter(l))), [ifilterfalse(pred, iter(l)).next()])
objs = [Object(name=""), Object(name="fake_name"), Object(name="")]
fn(objs, lambda o: o.name != '')

You can also make this deal with iterators rather than lists by changing things around a little. You also have some different imports.

from itertools import chain, islice, ifilter, ifilterfalse
fn = lambda l, pred: chain(ifilter(pred, iter(l)), islice(ifilterfalse(pred, iter(l)), 1))

You can always reoganize the code to reduce the length of the statements.


回答 6

如果代替flag = True我们可以代替导入,那么我认为这符合标准:

>>> from itertools import count
>>> a = ['hello', '', 'world', '', '', '', 'bob']
>>> filter(lambda L, j=count(): L or not next(j), a)
['hello', '', 'world', 'bob']

或者,过滤器最好写成:

>>> filter(lambda L, blank_count=count(1): L or next(blank_count) == 1, a)

或者,仅是一个简单的布尔值,没有任何导入:

filter(lambda L, use_blank=iter([True]): L or next(use_blank, False), a)

If instead of flag = True we can do an import instead, then I think this meets the criteria:

>>> from itertools import count
>>> a = ['hello', '', 'world', '', '', '', 'bob']
>>> filter(lambda L, j=count(): L or not next(j), a)
['hello', '', 'world', 'bob']

Or maybe the filter is better written as:

>>> filter(lambda L, blank_count=count(1): L or next(blank_count) == 1, a)

Or, just for a simple boolean, without any imports:

filter(lambda L, use_blank=iter([True]): L or next(use_blank, False), a)

回答 7

在迭代过程中跟踪状态的Python方法是使用生成器。itertools的方法很难理解恕我直言,而试图破解lambda来做到这一点显然是愚蠢的。我会尝试:

def keep_last_empty(input):
    last = None
    for item in iter(input):
        if item.name: yield item
        else: last = item
    if last is not None: yield last

output = list(keep_last_empty(input))

总体而言,可读性每次都比紧凑性好。

The pythonic way to track state during iteration is with generators. The itertools way is quite hard to understand IMHO and trying to hack lambdas to do this is plain silly. I’d try:

def keep_last_empty(input):
    last = None
    for item in iter(input):
        if item.name: yield item
        else: last = item
    if last is not None: yield last

output = list(keep_last_empty(input))

Overall, readability trumps compactness every time.


回答 8

不可以,由于其自​​身的定义,您无法将分配放入lambda中。如果使用函数式编程进行工作,则必须假定您的值不可更改。

一种解决方案是以下代码:

output = lambda l, name: [] if l==[] \
             else [ l[ 0 ] ] + output( l[1:], name ) if l[ 0 ].name == name \
             else output( l[1:], name ) if l[ 0 ].name == "" \
             else [ l[ 0 ] ] + output( l[1:], name )

No, you cannot put an assignment inside a lambda because of its own definition. If you work using functional programming, then you must assume that your values are not mutable.

One solution would be the following code:

output = lambda l, name: [] if l==[] \
             else [ l[ 0 ] ] + output( l[1:], name ) if l[ 0 ].name == name \
             else output( l[1:], name ) if l[ 0 ].name == "" \
             else [ l[ 0 ] ] + output( l[1:], name )

回答 9

如果您需要一个lambda来记住调用之间的状态,则建议您在本地命名空间中声明的函数或带有重载的类 __call__。既然我对您要执行的操作的所有警告都已解决,我们可以为您的查询提供实际答案。

如果确实需要让lambda在两次调用之间有一些内存,则可以将其定义为:

f = lambda o, ns = {"flag":True}: [ns["flag"] or o.name, ns.__setitem__("flag", ns["flag"] and o.name)][0]

然后,您只需传递f给即可filter()。如果确实需要,您可以找回价值flag使用以下方法:

f.__defaults__[0]["flag"]

或者,您可以通过修改的结果来修改全局命名空间globals()。不幸的是,您不能以与修改结果locals()不会影响本地命名空间相同的方式来修改本地命名空间。

If you need a lambda to remember state between calls, I would recommend either a function declared in the local namespace or a class with an overloaded __call__. Now that all my cautions against what you are trying to do is out of the way, we can get to an actual answer to your query.

If you really need to have your lambda to have some memory between calls, you can define it like:

f = lambda o, ns = {"flag":True}: [ns["flag"] or o.name, ns.__setitem__("flag", ns["flag"] and o.name)][0]

Then you just need to pass f to filter(). If you really need to, you can get back the value of flag with the following:

f.__defaults__[0]["flag"]

Alternatively, you can modify the global namespace by modifying the result of globals(). Unfortunately, you cannot modify the local namespace in the same way as modifying the result of locals() doesn’t affect the local namespace.


回答 10

您可以使用绑定函数来使用伪多语句lambda。然后,您可以将包装器类用于Flag以启用分配。

bind = lambda x, f=(lambda y: y): f(x)

class Flag(object):
    def __init__(self, value):
        self.value = value

    def set(self, value):
        self.value = value
        return value

input = [Object(name=""), Object(name="fake_name"), Object(name="")]
flag = Flag(True)
output = filter(
            lambda o: (
                bind(flag.value, lambda orig_flag_value:
                bind(flag.set(flag.value and bool(o.name)), lambda _:
                bind(orig_flag_value or bool(o.name))))),
            input)

You can use a bind function to use a pseudo multi-statement lambda. Then you can use a wrapper class for a Flag to enable assignment.

bind = lambda x, f=(lambda y: y): f(x)

class Flag(object):
    def __init__(self, value):
        self.value = value

    def set(self, value):
        self.value = value
        return value

input = [Object(name=""), Object(name="fake_name"), Object(name="")]
flag = Flag(True)
output = filter(
            lambda o: (
                bind(flag.value, lambda orig_flag_value:
                bind(flag.set(flag.value and bool(o.name)), lambda _:
                bind(orig_flag_value or bool(o.name))))),
            input)

回答 11

有点麻烦的解决方法,但是在lambda中进行分配无论如何都是非法的,因此这并不重要。您可以使用内置exec()函数从lambda内部运行分配,例如以下示例:

>>> val
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    val
NameError: name 'val' is not defined
>>> d = lambda: exec('val=True', globals())
>>> d()
>>> val
True

Kind of a messy workaround, but assignment in lambdas is illegal anyway, so it doesn’t really matter. You can use the builtin exec() function to run assignment from inside the lambda, such as this example:

>>> val
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    val
NameError: name 'val' is not defined
>>> d = lambda: exec('val=True', globals())
>>> d()
>>> val
True

回答 12

首先,您不需要为工作使用本地服务,只需检查以上答案

其次,使用locals()和globals()来获取变量表,然后更改值很简单

检查以下示例代码:

print [locals().__setitem__('x', 'Hillo :]'), x][-1]

如果您需要更改将全局变量添加到您的环境,请尝试将locals()替换为globals()

python的list comp很酷,但是大多数triditional项目不接受此命令(例如flask:[)

希望它可以帮助

first , you dont need to use a local assigment for your job, just check the above answer

second, its simple to use locals() and globals() to got the variables table and then change the value

check this sample code:

print [locals().__setitem__('x', 'Hillo :]'), x][-1]

if you need to change the add a global variable to your environ, try to replace locals() with globals()

python’s list comp is cool but most of the triditional project dont accept this(like flask :[)

hope it could help


Python中的表达式和语句有什么区别?

问题:Python中的表达式和语句有什么区别?

在Python中,表达式和语句之间有什么区别?

In Python, what is the difference between expressions and statements?


回答 0

表达式仅包含标识符文字运算符,其中的运算符包括算术运算符和布尔运算符,函数调用运算符 ()包括预订运算符 []等,并且可以简化为某种“值”,可以是任何Python对象。例子:

3 + 5
map(lambda x: x*x, range(10))
[a.x for a in some_iterable]
yield 7

声明(请参阅 1另一方面,2)是构成一行(或几行)Python代码的所有内容。注意表达式也是语句。例子:

# all the above expressions
print 42
if x: do_y()
return
a = 7

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. Examples:

3 + 5
map(lambda x: x*x, range(10))
[a.x for a in some_iterable]
yield 7

Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:

# all the above expressions
print 42
if x: do_y()
return
a = 7

回答 1

表达 -来自新牛津美国词典

expression(数学表达式):数学上共同表示一个数量的一组符号:圆的圆周的表达式为2πr。

总的来说:表达式产生至少一个值。

在Python中,表达式在Python语言参考中得到了广泛的介绍。通常,Python中的表达式由AtomsPrimaryOperators的语法合法组合组成。

维基百科的Python表达式

表达式示例:

操作符内置函数或用户编写的函数的调用之间的文字和语法正确组合:

>>> 23
23
>>> 23l
23L
>>> range(4)
[0, 1, 2, 3] 
>>> 2L*bin(2)
'0b100b10'
>>> def func(a):      # Statement, just part of the example...
...    return a*a     # Statement...
... 
>>> func(3)*4
36    
>>> func(5) is func(a=5)
True

维基百科的声明

在计算机编程中,可以将语句视为命令式编程语言中最小的独立元素。一个程序是由一个或多个语句的序列构成的。语句将具有内部组件(例如,表达式)。

维基百科的Python语句

总的来说:陈述做某事,通常由表达式(或其他陈述)组成

《 Python语言参考广泛涵盖了简单语句复合语句

但是,“语句做某事”和“表达式产生值”的区别可能变得模糊:

  • 列表推导被认为是“表达式”,但它们具有循环构造,因此也可以执行某些操作。
  • if通常是一个语句,如if x<0: x=0,但你也可以有一个条件表达式就像x=0 if x<0 else 1是表达式。在其他语言中,例如C,这种形式称为这样的运算符x=x<0?0:1;
  • 您可以通过编写函数来编写自己的表达式。def func(a): return a*a使用时为表达式,但定义时由语句组成。
  • 返回的表达式None是Python中的一个过程:从def proc(): pass语法上讲,您可以将其proc()用作表达式,但这可能是一个错误…
  • 对于表达式和语句之间的区别,Python比说C更严格。在C语言中,任何表达都是法律声明。您可以拥有func(x=2);“表达式”或“语句”吗?(答案:表达式用作带有副作用的语句)Python x=2中的函数调用内部的赋值语句仅在对C示例的调用func(x=2)中将命名参数设置a为2,func并且比C示例受到更多限制。

Expression — from the New Oxford American Dictionary:

expression: Mathematics a collection of symbols that jointly express a quantity : the expression for the circumference of a circle is 2πr.

In gross general terms: Expressions produce at least one value.

In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.

Python expressions from Wikipedia

Examples of expressions:

Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:

>>> 23
23
>>> 23l
23L
>>> range(4)
[0, 1, 2, 3] 
>>> 2L*bin(2)
'0b100b10'
>>> def func(a):      # Statement, just part of the example...
...    return a*a     # Statement...
... 
>>> func(3)*4
36    
>>> func(5) is func(a=5)
True

Statement from Wikipedia:

In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements. A statement will have internal components (e.g., expressions).

Python statements from Wikipedia

In gross general terms: Statements Do Something and are often composed of expressions (or other statements)

The Python Language Reference covers Simple Statements and Compound Statements extensively.

The distinction of “Statements do something” and “expressions produce a value” distinction can become blurry however:

  • List Comprehensions are considered “Expressions” but they have looping constructs and therfore also Do Something.
  • The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;
  • You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.
  • An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug…
  • Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The assignment statement of x=2 inside of the function call of func(x=2) in Python sets the named argument a to 2 only in the call to func and is more limited than the C example.

回答 2

尽管这与Python不相关:

一个expression值等于一个值。A statement做某事。

>>> x + 2         # an expression
>>> x = 1         # a statement 
>>> y = x + 1     # a statement
>>> print y       # a statement (in 2.x)
2

Though this isn’t related to Python:

An expression evaluates to a value. A statement does something.

>>> x + 2         # an expression
>>> x = 1         # a statement 
>>> y = x + 1     # a statement
>>> print y       # a statement (in 2.x)
2

回答 3

语句表示动作或命令,例如打印语句,赋值语句。

print 'hello', x = 1

表达式是变量,操作和值的组合,产生结果值。

5 * 5 # yields 25

最后,表达式语句

print 5*5

Statements represent an action or command e.g print statements, assignment statements.

print 'hello', x = 1

Expression is a combination of variables, operations and values that yields a result value.

5 * 5 # yields 25

Lastly, expression statements

print 5*5

回答 4

表达式是可以简化为值的内容,例如"1+3""foo = 1+3"

很容易检查:

print foo = 1+3

如果不起作用,则为语句;如果不起作用,则为表达式。

另一种说法可能是:

class Foo(Bar): pass

因为它不能减少为一个值。

An expression is something that can be reduced to a value, for example "1+3" or "foo = 1+3".

It’s easy to check:

print foo = 1+3

If it doesn’t work, it’s a statement, if it does, it’s an expression.

Another statement could be:

class Foo(Bar): pass

as it cannot be reduced to a value.


回答 5

  1. 表达式是返回值的语句。因此,如果它可以出现在赋值的右侧,或者作为方法调用的参数,则它是一个表达式。
  2. 根据上下文,某些代码可以是表达式也可以是语句。该语言可能有一种在模棱两可时区分它们的方法。
  1. An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.
  2. Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.

回答 6

表达式是某种东西,而语句则是某种东西。
表达式也是一个语句,但是它必须有一个返回值。

>>> 2 * 2          #expression
>>> print(2 * 2)     #statement

PS:解释器始终打印出所有表达式的值。

An expression is something, while a statement does something.
An expression is a statement as well, but it must have a return.

>>> 2 * 2          #expression
>>> print(2 * 2)     #statement

PS:The interpreter always prints out the values of all expressions.


回答 7

声明:

语句是执行某项操作的动作或命令。例如:If-Else,Loops..etc

val a: Int = 5
If(a>5) print("Hey!") else print("Hi!")

表达:

表达式是值,运算符和文字的组合,产生某些结果。

val a: Int = 5 + 5 #yields 10

STATEMENT:

A Statement is a action or a command that does something. Ex: If-Else,Loops..etc

val a: Int = 5
If(a>5) print("Hey!") else print("Hi!")

EXPRESSION:

A Expression is a combination of values, operators and literals which yields something.

val a: Int = 5 + 5 #yields 10

回答 8

语句包含关键字。

表达式不包含关键字。

print "hello"是语句,因为print是关键字。

"hello" 是一个表达式,但是列表压缩与此相反。

以下是一个表达式语句,没有列表理解也是如此:

(x*2 for x in range(10))

A statement contains a keyword.

An expression does not contain a keyword.

print "hello" is statement, because print is a keyword.

"hello" is an expression, but list compression is against this.

The following is an expression statement, and it is true without list comprehension:

(x*2 for x in range(10))

回答 9

表达式:

  • 通过组合objects和来形成表达式operators
  • 表达式具有一个值,该值具有一个类型。
  • 简单表达式的语法:<object><operator><object>

2.0 + 3是一个表达式,其结果为,5.0并且具有float与之关联的类型。

陈述

语句由表达式组成。它可以跨越多行。

Expressions:

  • Expressions are formed by combining objects and operators.
  • An expression has a value, which has a type.
  • Syntax for a simple expression:<object><operator><object>

2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.

Statements

Statements are composed of expression(s). It can span multiple lines.


回答 10

有些语句可能会改变我们Python程序的状态:创建或更新变量,定义函数等。

表达式仅返回一些值,无法更改函数中的全局状态或局部状态。

但是现在我们:=知道了,它是外星人!

There are some statements which could change the state of our Python program: create or update variables, define function, etc.

And expressions just return some value can’t change the global state or local state in a function.

But now we got :=, it’s an alien!


回答 11

Python将表达式称为“表达式语句”,因此问题可能尚未完全形成。

语句几乎包含您可以在Python中执行的所有操作:计算值,分配值,删除变量,打印值,从函数返回,引发异常等。完整列表在此处:http:// docs.python.org/reference/simple_stmts.html#

表达式语句仅限于调用函数(例如math.cos(theta)“),运算符(例如“ 2 + 3”)等以产生值。

Python calls expressions “expression statements”, so the question is perhaps not fully formed.

A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#

An expression statement is limited to calling functions (e.g., math.cos(theta)”), operators ( e.g., “2+3”), etc. to produce a value.


回答 12

我认为表达式包含运算符+操作数和保存运算结果的对象…例如

var sum = a + b;

但是一条语句只是一行代码(可能是一个表达式)或代码块…例如

fun printHello(name: String?): Unit {
if (name != null)
    println("Hello ${name}")
else
    println("Hi there!")
// `return Unit` or `return` is optional

}

I think an expression contains operators + operands and the object that holds the result of the operation… e.g.

var sum = a + b;

but a statement is simply a line of a code (it may be an expression) or block of code… e.g.

fun printHello(name: String?): Unit {
if (name != null)
    println("Hello ${name}")
else
    println("Hi there!")
// `return Unit` or `return` is optional

}