lambda和常规函数之间的python有什么区别? [英] what is the difference for python between lambda and regular function?

查看:159
本文介绍了lambda和常规函数之间的python有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇lambda函数和常规函数(用def定义)之间的区别-在python级别. (我知道对程序员有什么区别,何时使用每个人.)

I'm curious about the difference between lambda function and a regular function (defined with def) - in the python level. (I know what is the difference for programmers and when to use each one.)

>>> def a():
    return 1

>>> b = lambda: 1
>>> a
<function a at 0x0000000004036F98>
>>> b
<function <lambda> at 0x0000000004031588>

我们可以看到-python 知道blambda函数,而a是常规函数.这是为什么? 与python 有什么区别?

As we can see - python knows that b is a lambda function and a is a regular function. why is that? what is the difference between them to python?

推荐答案

它们是相同的类型,因此它们的处理方式相同:

They are the same type so they are treated the same way:

>>> type(a)
<type 'function'>
>>> type(b)
<type 'function'>

Python还知道b被定义为lambda函数,并将其设置为函数名称:

Python also knows that b was defined as a lambda function and it sets that as function name:

>>> a.func_name
'a'
>>> b.func_name
'<lambda>'

换句话说,它影响了该函数将获得的名称,但是就Python而言,两者都是函数,这意味着它们大多数都可以以相同的方式使用.有关酸洗的功能和lambda函数之间的重要区别,请参见下面的mgilson评论.

In other words, it influences the name that the function will get but as far as Python is concerned, both are functions which means they can be mostly used in the same way. See mgilson's comment below for an important difference between functions and lambda functions regarding pickling.

这篇关于lambda和常规函数之间的python有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆