Content deleted Content added
Notification of altered sources needing review #IABot (v1.5.4) |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 135:
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:
<
>>> code="""def myfunc(a):
Line 148:
>>> myfunc(3.0)
3.0 is not an integer number
>>></
(just tested using Python 2.5.2) --[[User:TobiasHerp|Tobias]] ([[User talk:TobiasHerp|talk]]) 09:55, 19 August 2008 (UTC)
Line 154:
And what about this? ->
<
import math
Line 168:
# cos(math.pi/2) ~> 0.0
</syntaxhighlight>
== Regarding runtime generation ==
Line 220:
== Higher-order functions in Perl ==
(see M.J. Dominus. Higher Order Perl, 2005. pp 325, 333)
<
$lambda = sub {$_[0] + 4};
print $lambda->(2), "\n"; # => 6
</syntaxhighlight>
[[User:Psilva|Psilva]] ([[User talk:Psilva|talk]]) 09:35, 26 September 2009 (UTC)
Line 289:
In Python:
<
>>> def b(): pass
Line 300:
{<function a at 0x0000000003266A48>, <function b at 0x00000000032669C8>}
>>> type({a, b})
<class 'set'></
: In Haskell the keys of a map need to be of a finite type in order to guarantee that the the equality function will terminate (or more pessimistically, you cannot even define and equality function for functions in Haskell). The function type is infinite. Does Python compare function pointers? And does it work in combination with anonymous functions? —''[[User:Ruud Koot|Ruud]]'' 07:46, 17 February 2011 (UTC)
<
>>> b = lambda : None
>>> type(a)
Line 319:
False
>>> a is a
True</
It seems as if first classness of functions comes down to:
Line 335:
: For your peace of mind: Python has first-class functions and reference equality:
<
>>> def main():
... a = 10
Line 348:
>>> (lambda x: x) == (lambda x: x)
False
</syntaxhighlight>
: —''[[User:Ruud Koot|Ruud]]'' 11:32, 17 February 2011 (UTC)
: Also Pythons nested functions seem crippled compared to Scheme:
<
>>> def main():
... x = 1
Line 366:
File "<stdin>", line 4, in f
UnboundLocalError: local variable 'x' referenced before assignment
</syntaxhighlight>
: but only artificially and not fatally:
<
>>> def main():
... x = [1]
Line 385:
>>> o()
5
</syntaxhighlight>
: —''[[User:Ruud Koot|Ruud]]'' 11:57, 17 February 2011 (UTC)
Hi Ruud, Python 3 addressed the access to outer variables issue with the nonlocal keyword:
<
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "copyright", "credits" or "license()" for more information.
Line 407:
>>> f()
4
>>> </
== Partial Function Application in C++ ==
|