Re:How To Let People Know You're A Bad Python Programmer
by blasdel on Friday August 28, 2009 @03:16PM
(#792056)
Of course functions are first-class, and the lambda statement does return anonymous functions. Functions in Python are just objects that have a __call__ method, and methods are just functions partially-applied to the object they are bound on -- LOVELY METACIRCULARITY
What aren't first class are statements and suites (indented blocks of expressions) -- they're part of the syntax and thus can't be constructed or referred to. Particularly nasty is the way non-value-returning statements can't be used in expressions passed to other statements.
Your example is perfect -- in Python < 3, print is a statement and not a built-in function!
# this is invalid syntax:
name_printer = lambda x: lambda: print x
# with print as a function it works:
from __future__ import print_function
name_printer = lambda x: lambda: print(x)
Re:How To Let People Know You're A Bad Python Programmer
by erlanger on Friday August 28, 2009 @03:32PM
(#792090)
Nice explanation. Note that I did wrap my print arg in parens, however. When I heard they were changing it to a function in 3 I made this a habit.
Re:How To Let People Know You're A Bad Python Programmer
by blasdel on Friday August 28, 2009 @04:04PM
(#792171)
Using parens that way with the print statement is a bad idea. You aren't using it like a function -- the parens are parsed as an expression.
statement function
print(1) 1 1
print(1,2) (1, 2) 1 2
print(1,2), 3 (1, 2) 3 SyntaxError
Just use the old syntax, and let 2to3 take care of you when the time comes.
I'm not sure I follow. This looks first-class to me.
Python doesn't support creating anonymous functions, but functions are first-class. Sorry if you already knew this; it seems like you know Python pretty well so I'm sure you do.