How To Let People Know You're A Bad Python Programmer
artificialcode.blogspot.com
artificialcode.blogspot.com
This is an oldie but a goodie, but a lot of bad programmers don't realize they can make their Python code even worse, if they embed a bunch of untested, legacy, Perl code in it. There is a Perl/Python module that makes this a snap.
Yes, throw in a Perl slight for good measure. A surer way of endearing yourself to your fellow Pythonistas, I know of not. This is not unlike what happens on reddit.com/r/programming, where an opportunity to sneer at Java is never missed, and every kind soul you encounter will gladly regale you with tales of the imaginary programs they claim to have written in Haskell or Lisp.
Perl, that miserable little unwanted orphan of a language, has real anonymous, first-class functions complete with closures. Python, sadly, does not, and that is an undeniable, glaring defect in the language. And for what reason? Because its Benevolent Dictator for Life decreed that functional programming--even in the greatly-diluted form in which one finds it in mainstream languages--is bad and "unpythonic"--heretical, essentially. He recently lashed out against tail call optimization, and before that attempted to exile some Lisp constructs that had been resident in the language for many years, but back-peddled as soon as his subjects threatened to revolt.
When Python catches up to Perl, Ruby, Javascript and every other decent dynamic language in this department, then you can condemn Perl as much as you like. Until that time, hold your tongue.
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 missing out on? (For that matter, I still haven't seen what TCO offers that is new, but that was April's issue.)
[edit: http://gmarceau.qc.ca/blog/2009/05/speed-size-and-dependabil... indicates that it doesn't bring anything- any counter offers?]
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 actually seen someone embed Perl (or any other language) in Python? Why is that admonition sandwiched between remarks on unit testing and maintaining control flow?
On another note, everyone seems to love anonymous, first-class functions with closures, and yet they never give nontrivial examples - what am I missing out on?
Spend some time with a language that not only supports them but actually encourages their use, and you will see for yourself. For example, both Smalltalk and Ruby make heavy use of blocks, and because of that classes can be written to allow users to greatly customize their behavior without the need of subclassing.
(For that matter, I still haven't seen what TCO offers that is new, but that was April's issue.)
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.)
The main reason Guido and his Pythonistas have no use for TCO is that they also have no use for recursion. If Python supported TCO, more recursive Python code would probably be written--and that would be very unpythonic.
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.
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.
It's like the difference between using objects in Smalltalk and writing your own object system in C.
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. Since statements cannot contain other statements outside of suites, it's impossible to implement full-fledged anonymous functions in the language grammar without breaking something.
While I'd love TCO in python, I like the stack traces better (ever dealt with a stray head [] in GHC?).
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 < 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) 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.Which Python are you talking about? The language I use has had closures and first-class functions for years.
>>> 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 "<stdin>", line 1, in <module>
File "<stdin>", line 5, in foo
File "<stdin>", line 4, in wibble
UnboundLocalError: local variable 'blah' referenced before assignment
Oh. Maybe not.I agree it's broken, but it's a different flaw than the closure problem.
First, why would you do his in a language where the idiomatic way to express that a function has failed is to throw an exception? Second, this is a pretty extreme failure and probably rare in (useful) Python code; why complain about about this when there is a real, extremely common problem in Python programming: not documenting your exceptions, so users who want to distinguish between different failure modes are forced to experiment and/or read your code.
A coworker found that calling out to grep can be faster than using Python's regular expressions for large amounts of text processing. My first reaction was to cringe, but it was indeed much faster for this particular problem; I think I was the bad programmer for reflexively cringing instead of saying "nice performance hack".
Why single out .NET? Am I missing something?
But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment.Language-wide style conventions make it easier to remember methods and classes since remembering a method like each_with_index as a phrase is easy, attaching a piece of useless data unrelated to what it does like Camel Case vs underscore is harder.
The answer to any "who says" question about Python is "Guido van Rossum says". (In the case of PEP 8, Barry Warsaw says as well.)
i too think the distinction between camel case and lowercase underscore as one being somehow more readable or better than the other is a little silly.
These are XXX comments and they're pretty common even with the best developers. They're so common that pretty much every editor has a syntax highlighting regex to detect them.