Talk:First-class function: Difference between revisions

Content deleted Content added
TobiasHerp (talk | contribs)
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 ==
== Python has first-class functions ==
 
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:
Python ''has'' first-class functions, hasn't it? Functions share the same namespace as variables; a function can be assigned to a variable (while of course it is more common to assign the result of a function call). The point about Python's <tt>lambda</tt> functions is them to be ''anonymous''; they are defined in-place where they are needed, e.g. in a call to the <tt>sort</tt> function, and are limited to contain just an expression. Or did I misunderstand something? --[[User:TobiasHerp|Tobias]] ([[User talk:TobiasHerp|talk]]) 09:34, 19 August 2008 (UTC)
 
<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)