Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82171
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Question on lambdas |
| Date | 2014-12-08 21:09 -0500 |
| References | <m65d2m$3pe$1@ger.gmane.org> <mailman.16745.1418083098.18130.python-list@python.org> <548653EC.5080107@gmx.de> <CAPTjJmodWsbyFC__V-hL8izP7G0jL8254vpAKL7rq4qih+Sy4g@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16755.1418091001.18130.python-list@python.org> (permalink) |
On 12/8/2014 8:53 PM, Chris Angelico wrote:
> On Tue, Dec 9, 2014 at 12:44 PM, Christoph Becker <cmbecker69@gmx.de> wrote:
>> Ben Finney wrote:
>>
>>> It's best to remember that ‘lambda’ is syntactic sugar for creating a
>>> function; the things it creates are not special in any way, they are
>>> normal functions, not “lambdas”.
>>
>> Could you please elaborate why ‘lambda’ does not create “lambdas”. I'm
>> a Python beginner (not new to programming, though), and rather confused
>> about your statement.
>
> For the same reason that "def" doesn't create "defs", and "for"
> doesn't create fors (uhh... the Force?). Both "lambda" and "def"
> create functions. Functions are things that can be called, and they're
> created by the "def" statement, the "lambda" expression, and very VERY
> occasionally, by directly calling the function constructor. You could
> distinguish between "lambda functions" and "def functions" if you
> like, but the distinction is almost never significant. In fact,
> usually you don't even need to distinguish between functions and other
> callables (types, objects with __call__ methods, bound method objects,
> etc, etc, etc).
To exemplify Chris's answer: Consider
>>> def f1(a, b='hi'): return a+b
>>> f2 = lambda a, b='hi': a+b
>>>
I believe the only difference between any of the attributes of f1 and f2 is
>>> f1.__name__
'f1'
>>> f2.__name__
'<lambda>'
>>> f1.__code__.co_name
'f1'
>>> f2.__code__.co_name
'<lambda>'
All other values of corresponding attributes of f1 and f2, and
f1.__code__ and f2.__code__, should be the same. For instance
>>> f1.__code__.co_varnames
('a', 'b')
>>> f2.__code__.co_varnames
('a', 'b')
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Question on lambdas Ben Finney <ben+python@benfinney.id.au> - 2014-12-09 10:58 +1100
Re: Question on lambdas Christoph Becker <cmbecker69@gmx.de> - 2014-12-09 02:44 +0100
Re: Question on lambdas Steven D'Aprano <steve@pearwood.info> - 2014-12-09 06:38 +0000
Re: Question on lambdas Chris Angelico <rosuav@gmail.com> - 2014-12-09 12:53 +1100
Re: Question on lambdas Terry Reedy <tjreedy@udel.edu> - 2014-12-08 21:09 -0500
Re: Question on lambdas Ben Finney <ben+python@benfinney.id.au> - 2014-12-09 16:23 +1100
Re: Question on lambdas "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-09 13:43 +0100
Re: Question on lambdas Rustom Mody <rustompmody@gmail.com> - 2014-12-08 22:44 -0800
csiph-web