'lambda fuction'에 해당되는 글 1건

  1. 2010.01.22 파이썬 람다 폼 - python lambda form/function (2)
솔찍히 이해를 못한 부분인데, 간단한 함수를 만드는 것 같다.
함수 포인터 같기도 한데 LISP에서 따왔다고도 하는데 먼소리인지 안드로메다로.

Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda".

>>> def f (x): return x**2
>>> print f(8)
>>> g = lambda x: x**2
>>> print g(8)

As you can see, f() and g() do exactly the same and can be used in the same ways. Note that the lambda definition does not include a "return" statement -- it always contains an expression which is returned. Also note that you can put a lambda definition anywhere a function is expected, and you don't have to assign it to a variable at all. 

[링크 : http://www.secnetix.de/olli/Python/lambda_functions.hawk]

4.7.5. Lambda Forms

By popular demand, a few features commonly found in functional programming languages like Lisp have been added to Python. With the lambda keyword, small anonymous functions can be created. Here’s a function that returns the sum of its two arguments: lambda a, b: a+b. Lambda forms can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda forms can reference variables from the containing scope:

>>> def make_incrementor(n):
... return lambda x: x + n
>>> f = make_incrementor(42)
>>> f(0)
>>> f(1)

[링크 : http://docs.python.org/tutorial/controlflow.html]

>>> f
<function <lambda> at 0xb754da3c>

위의 예제 실행후 f만 입력하면 위와 같이 fuction <lambda> 라는 말이 나온다.
일종의 간단한 return이 필요없는 함수 - c에서 inline 함수? - 가 되는 것처럼 보인다.
Posted by 구차니

댓글을 달아 주세요