Content deleted Content added
TobiasHerp (talk | contribs) →Python has first-class functions: new section |
TobiasHerp (talk | contribs) |
||
Line 45:
: I must concur. [[User:Jsnx|jsnx]] ([[User talk:Jsnx|talk]]) 02:11, 7 December 2007 (UTC)
== Function literals in Python ==
IMO Python ''has'' unlimited function literals; it is possible to create any function (not limited to an expression like <tt>lambda</tt> functions) by creating a string and executing it; example:
<source lang="python">
>>> code="""def myfunc(a):
... if isinstance(a, int):
... print a**2
... else:
... print a, 'is not an integer number'
... """
>>> exec(code)
>>> myfunc(3)
9
>>> myfunc(3.0)
3.0 is not an integer number
>>></source>
(just tested using Python 2.5.2) --[[User:TobiasHerp|Tobias]] ([[User talk:TobiasHerp|talk]]) 09:55, 19 August 2008 (UTC)
|