简体   繁体   中英

Lambda and functions in Python

I have beginner two questions

  1. What does *z or *foo or **foo mean regarding function in Python.
  2. This works - a = lambda *z :z But this does not - a = lambda **z: z . Because it is supposed to take 0 arguments. What does this actually mean?

*z and **z in Python refer to args and kwargs. args are positional arguments and kwargs are keyword arguments. lambda **z doesn't work in your example because z isn't a keyword argument: it's merely positional. Compare these different results:

    >>> a = lambda z: z
    >>> b = lambda *z: z
    >>> c = lambda **z: z
    >>> a([1,2,3])
    [1, 2, 3]
    >>> b([1,2,3])
    ([1, 2, 3],)
    >>> c([1,2,3]) # list arg passed, **kwargs expected
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: <lambda>() takes exactly 0 arguments (1 given)
    >>> c(z=[1,2,3]) # explicit kwarg
    {'z': [1, 2, 3]}
    >>> c(x=[1,2,3]) # explicit kwarg
    {'x': [1, 2, 3]}
    >>> c({'x':[1,2,3]}) # dict called as a single arg
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: <lambda>() takes exactly 0 arguments (1 given)
    >>> c(**{'x':[1,2,3]}) # dict called as **kwargs
    {'x': [1, 2, 3]}
    >>> b(*[1,2,3]) # list called as *args
    (1, 2, 3)

查看链接,这是一篇关于如何在Python中使用* args和** kwargs的好博客文章http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in -蟒蛇/

When you see something like def foo(*args) or def foo(**kwargs) they're telling you they're expecting something more than a single argument. They're expecting several arguments or several named arguments. Often, however, you see this:

def foo(item1, item2, item3, *args, **kwargs)

Which tells you that they're expecting at least item's 1, 2, and 3 but "args" and "kwargs" are optional parameters.

To answer your question regarding a = lambda **z: z try passing in a "named argument list" like so:

> a = lambda **z:z
> a(b=1, c=2)
>> {b : 1, c : 2}

The output will be a dictionary, just as you inadvertently defined. Named argument lists are, in essence, dictionaries.

*args can be used to make a function take an arbitrary number of arguments. **kwargs can be used to make a function take arbitrary keyword arguments. This means that:

>>> foo = lambda *args: args
>>> bar = lambda **kwargs: kwargs
>>> foo(1,2,3)
(1,2,3)
>>> bar(asdf=1)
{'asdf':1}
>>> bar(1,2,3)
TypeError: <lambda>() takes exactly 0 arguments (3 given)

The last error happens because bar can only take keyword arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM