Earlier quoted context omitted.
Perl, that miserable little unwanted orphan of a language, has real anonymous, first-class functions complete with closures. Python, sadly, does not Which Python are you talking about? The language I use has had closures and first-class functions for years.
They don't seem to work very well: >>> def foo(): ... blah = 42 ... def wibble(): ... print "blah is " + blah ... wibble() ... >>> foo() blah is 42 Great! Looks like we have real closures! >>> def foo(): ... blah = 42 ... def wibble(): ... blah += 1 ... wibble() ... print "blah is " + blah ... >>> foo() Traceback (most recent call last): File " ", line 1, in File " ", line 5, in foo File " ", line 4, in wibble Unboun…
How To Let People Know You're A Bad Python Programmer
31–40 of 40 posts
Re: How To Let People Know You're A Bad Python Programmer
#32Re: How To Let People Know You're A Bad Python Programmer
#33Earlier quoted context omitted.
I agree wholeheartedly with the first half of your comment, but the second half is the same uninformed dogma you rail against in the first! Python is not really a functional programming language -- it has statements and suites, neither of which are first-class. lambda is implemented as an inline (suite-less) value-returning statement (like the ternary statement) because dealing with indentation would be a bitch. Sinc…
> first-class. I'm not sure I follow. This looks first-class to me. def name_printer(name): def printer(): print(name) return printer erlanger_printer = name_printer('erlanger') 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.
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
# 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
#34Earlier quoted context omitted.
Why call Perl from Python? Why not just do everything in Perl? This is an awful lot of invective from one paragraph that, personally, implies an itinerant bad programmer migrating from 'cool language' to 'new cool language' and carting along everything he'd written before. On another note, everyone seems to love anonymous, first-class functions with closures, and yet they never give nontrivial examples - what am I mi…
This is an awful lot of invective from one paragraph that, personally, implies an itinerant bad programmer migrating from 'cool language' to 'new cool language' and carting along everything he'd written before. If he had meant only that and meant nothing more, he wouldn't have singled out Perl, or he would have done so in a more tactful manner. And ask yourself, is this really a big problem? How often have you actual…
If Python supported TCO, more recursive Python code would probably be written
And not be able to run on Python implementations without TCO!Code that takes advantage of TCO is utterly dependent on it. It is absolutely not an 'implementation detail', or really an 'optimization' -- its presence is a major part of the semantics of any language implementation that does it.
Re: How To Let People Know You're A Bad Python Programmer
#35Earlier quoted context omitted.
> first-class. I'm not sure I follow. This looks first-class to me. def name_printer(name): def printer(): print(name) return printer erlanger_printer = name_printer('erlanger') 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.
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 referr…
Re: How To Let People Know You're A Bad Python Programmer
#36Earlier quoted context omitted.
Why call Perl from Python? Why not just do everything in Perl? This is an awful lot of invective from one paragraph that, personally, implies an itinerant bad programmer migrating from 'cool language' to 'new cool language' and carting along everything he'd written before. On another note, everyone seems to love anonymous, first-class functions with closures, and yet they never give nontrivial examples - what am I mi…
This is an awful lot of invective from one paragraph that, personally, implies an itinerant bad programmer migrating from 'cool language' to 'new cool language' and carting along everything he'd written before. If he had meant only that and meant nothing more, he wouldn't have singled out Perl, or he would have done so in a more tactful manner. And ask yourself, is this really a big problem? How often have you actual…
This isn't an example and does nothing to convince me that it's actually useful. Your suggested application just hints at difficult debugging when a Car object can be a stealth Truck. Maybe it isn't actually difficult, but why is this actually better than subclassing?
TCO allows programs to, in certain circumstances, reuse existing stack frames for possibly many function invocations. For example, if a function is written to be tail recursive, a VM with TCO could reuse the same stack frame for each recursive function invocation until the terminating condition is reached, at which point the last return value is caught and the frame is popped off the stack. (TCO isn't strictly limited to making tail recursion more efficient, but that is its most often cited benefit.)
Sounds like a loop.
Re: How To Let People Know You're A Bad Python Programmer
#37Earlier quoted context omitted.
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 referr…
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.
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.Re: How To Let People Know You're A Bad Python Programmer
#38Earlier quoted context omitted.
Only since Python 3000, with the addition of the "nonlocal" declaration, can python claim to have closures. And yes, Python does also have "first-class functions"--in the same way that C has first-class functions. It's the "anonymous" part that it has difficulty with.
Glad to know I can add you to my list of people who believe that Haskell does not (and by definition cannot) have closures.
Re: How To Let People Know You're A Bad Python Programmer
#39Earlier quoted context omitted.
This is an awful lot of invective from one paragraph that, personally, implies an itinerant bad programmer migrating from 'cool language' to 'new cool language' and carting along everything he'd written before. If he had meant only that and meant nothing more, he wouldn't have singled out Perl, or he would have done so in a more tactful manner. And ask yourself, is this really a big problem? How often have you actual…
Spend some time with a language that not only supports them but actually encourages their use, and you will see for yourself. This isn't an example and does nothing to convince me that it's actually useful. Your suggested application just hints at difficult debugging when a Car object can be a stealth Truck. Maybe it isn't actually difficult, but why is this actually better than subclassing? TCO allows programs to, i…
It's like the difference between using objects in Smalltalk and writing your own object system in C.
Re: How To Let People Know You're A Bad Python Programmer
#40Earlier quoted context omitted.
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.
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.