Joe Stump: Why I switched from PHP to Python
joestump.net
joestump.net
* TCO is dangerous and hard, let's not support it in the language
* ++ is ugly, let's not support it in the language
* Assignment during comparison leads to bugs, let's not support it in the language
* Python gives you more than enough rope to hang yourself, but at least hanging yourself is an option.
One of these things is not like the others.I coded too much in C. I like all four!
BTW as someone that moved from Pascal to C and C++, I can appreciate the ugliness of a lot of C shorthand syntax. It is confusing and can cause a lot of bizarre and hard to catch bugs.
"Second, the idea that TRE is merely an optimization, which each Python implementation can choose to implement or not, is wrong. " - http://neopythonic.blogspot.com/2009/04/tail-recursion-elimi...
"Second, the idea that TRE is merely an optimization, which each Python implementation can choose to implement or not, is wrong. Once tail recursion elimination exists, developers will start writing code that depends on it, and their code won't run on implementations that don't provide it: a typical Python implementation allows 1000 recursions, which is plenty for non-recursively written code and for code that recurses to traverse, for example, a typical parse tree, but not enough for a recursively written loop over a large list." - http://neopythonic.blogspot.com/2009/04/tail-recursion-elimi...
Stack traces are part of the Python language (besides looking pretty, the traceback module lets you inspect them), so it'd be really hard for a conforming implementation to include TCO - at least the type that actually has tail recursion run in constant space.
Java faces a similar problem with HotSpot's inlining, which is why you occasionally get <compiled code> in a Java stack trace instead a line and file number. They seem to have some hack in to avoid eliding the stack frame entirely though. This is probably what I'd do if I were designing language: push a bare-bones stack frame with just the debug info, but pop all the unused temporary working storage. Or punt on general TCO and just optimize tail-recursion - isn't that what Python was proposing to do with the @tailcall decorator anyway?
The languages I know of with TCO - gcc and Scheme - have really terrible debugging support. IMNSHO, I think that giving up debugging info for TCO is a poor tradeoff.
"Can I do this in Python?"
than
"I already did this in Python!"
Also, last time I checked the programming language shootout, Perl 5.10 is slightly faster than Python 3.
So with that in mind, the logical "step up" from Perl would be something like Haskell. Python is pretty much Perl with different syntax, less features, and the impression that it's "cleaner" because nobody ever bothered using it for "quick and dirty" scripting.
I can't read my Perl code from six months ago like I can read my Python code ages ago.
There are far too many ways to do the same thing in Perl, not every one of them obvious. And I disagree with my past self a lot.
unreadable for the claimants not for you.
"Go find me perl I can't read .. Where is it?"
Any language, no matter how obscure, will have some practitioners who can read any code written in it.
I am sure someone can say this about APL "What do you mean APL is unreadable? I can read any APL code fragment".
To repeat, they are saying they can't read it not that you can't. Don't take it personally. Use what works for you.
Anyone who has studied those two books can understand 99%+ of the code on the CPAN.
A lot of people have made a few halfhearted attempts at using Perl and were so put off by the scary looking sigils that they gave up. Then they blame Perl to comfort themselves. It's not them that's too lazy to look past a new kind of syntax, it's the language's fault!
again replace "perl" with "APL" or "BrainFuck" (or any language with baroque syntax) and the above sentence is as (in)valid. This comes close to the "No True Scotsman" fallacy. If you really knew perl you would have no problems with reading it. Sure, but that applies to anything, no matter how convoluted.
It doesn't matter whether you think their claims are "bogus" or not, or whether they meet your standards of "really" knowing perl.
Some people find that they can read their own code in other languages easier than their own code in perl after 6 months. You might think this "bogus", that doesn't matter. For them, that is a reality they have to deal with. And many deal with it by shifting to other languages and leaving perl to its aficionados. This is as it should be. Everyone should use the language they like and not use languages they dislike (irrespective of my or your stances on what they should like or whether their claimed reasons for dislike is "bogus").
Nobody is compelling you to abandon perl. So what's your problem with them not using perl?
A language is a UI for programmers. Some people find some UIs hard. Telling them that their discomfort is "bogus" is not very useful.
I use a lot of Haskell. Many people say they find its type system, monads etc "too hard". That isn't a "bogus claim". And "If they really knew Haskell they wouldn't find it hard" (where I define the "really knew") is a useless statement.
I use Haskell, they don't. Everyone is happy.
Live and let live. There are enough languages for all of us.
Some people think Perl is a good deal more expressive and powerful for them. A whole lot more people would if they managed to get past their initial reaction.
Python probably owes most of its success to its simplistic syntax. That is definitely a feature when you're learning. When you're going to spend years with a tool though it's often worth investing some time into new concepts.
More people should probably learn Haskell if they want to be better programmers. I think more should learn Perl too.
This is, no doubt, true. But if people find that for a lesser investment (in say Python or Ruby) they can get all the (or almost all the) benefits of learning perl, it is natural they prefer those languages.
If a hypothetical English speaker were to be given the choice of learning French or Japanese and if he could get his work done with either language equally well (this is where programming languages differ from spoken languages) why is it a problem that someone chooses to learn French (an English speaker could be fluent in french in year or so, Japanese would take years) instead of Japanese? A Chinese speaker might prefer to learn Japanese vs French and that's fine too.
Focussing on perl specifically, unless I am maintaining a legacy codebase or I have a situation where libraries I need exist only in perl (and not, say in Python) why should I learn perl in preference to (say) Python?
I can imagine making a case for learning Haskell in preference to Python (or vice versa!) but perl Python and Ruby are largely equivalent in practice and at least to some people Python is easier to read than perl.
"Some people think Perl is a good deal more expressive and powerful for them. A whole lot more people would if they managed to get past their initial reaction."
I agree. The counterpoint is that there are equally powerful and expressive languages where I don't need to get past the initial reaction. To some people aesthetics matter. And aesthetics is subjective.
It's tiresome seeing the same old superficial argument about Perl's sigils. I'd like to see more discussion about how much more powerful and advanced Perl's Moose object system is than Python's :-)
:-) I agree this would be a fruitful line of discussion. I know next to nothing about Perl and while (as of now) I have no intention of learning Perl, it would be great if someone (you? :-D ) were to write (or submit) such a post to HN
The more vocal the advocates the more the it feels as if the choice is no longer free, that will be reflected in a resistance to observe the language objectively, some of the feeling has already been spoiled by the abrasive attitude of the proponents. Erlang suffered quite a bit from that when people started spamming HN with Erlang articles to flood other items off the new page. It could be an excellent language, but in the eyes of some it is a language associated with anti-social behaviour.
If perl were easier to learn it would lose some of its power, so maybe it is not possible to have a language that fits the bill for everybody.
These trade-offs are to be expected and are in no way a detraction, that is why we have many different programming languages in the first place. One size does not fit all.
With this in mind, some advocacy is necessary to steer the field in a direction that is pleasing to those that know more. The advocates are just trying to help these people get more out of programming than they are going to get from $simple_language (Java, PHP).
The problem, however, is that many of the advocates are advocating because they randomly picked programming language $foo, think it's good enough, and have never tried anything else. Their advocacy is designed to recruit other followers to justify their own decisions. And that is very silly.
It is especially amusing when this type of advocate attacks the first kind of advocate. The argument is always, "well, but my code works". "Erlang works for phone switches!" "PHP can talk to a MySQL database!" Well, OK, but are you positive those things couldn't be done better in a different language? (The answer is usually, "uh, well, no...", but I digress.)
There seems to be an awful lot of that happening.
Don't shoot me, but I also observe a similar symptom with high-priced and/or hard-to-move-away-from "corporate" (as in "boring") products. The more your company invested in said product, the more successful the enterprise will show up on internal communications, with little or no correlation however badly it failed.
People get attached to their investments, be they money, time or self-esteem. Or, in the corporate case, the jobs of the people who signed the check.
As for Python, as a long time pythonista, I can tell you there are many steps to Python-zen, exactly like what happens with Perl (I did a lot of Perl between 95 and 2002). The simplistic syntax hides a lot of power that elude the newbie. When I review my early code, I see it as childish and crude.
If someone wants to learn new languages, I always advise them to learn something as different and "alien" from his/her comfort zone as possible. Haskell is a good choice, a Lisp like Clojure or Scheme is another. And by learn I mean taking a couple years as a side hobby to do increasingly difficult problems, perhaps some you already took with your comfortable-set. It _has_ to be an enlightening experience or it loses the purpose.
Python taught me that there's no excuse for a language to have a learning curve like Perl's.
I'm really not a Perl fan, but I really don't buy readability arguments. I'm a functional programmer, and the exact same feeble criticisms are leveled against ML and Haskell and Lisp and Scheme and whatever else. People say "ugh! It's so unreadable!", when they really mean "I can't read that having spent 2 days learning this new language, but I find it very easy to read C/PHP/Java/whatever, which I've been doing for several years".
It can take a very, very long time to actually become fluent in a programming language and paradigm to the point where you actually have a mental model of its semantics that is aligned with reality. This is why rookie programmers tend to get things wrong.
I spent almost 6 months learning Prolog. If I went and tried to write prolog now, I doubt I could remember very much at all. I've done C for more like 10 years, and I can still write C reliably despite not having touched it for 2 or 3 years.
So I don't buy it. I'm really not a Perl fan (nor a Python fan), but the idea that it's worse than python because it requires more intellectual effort to learn is bullshit, unless you're having a programming-for-people-of-limited-intelligence competition.
And my standards for a programming language's learning curve applies to experienced programmers. It's just a happy coincidence that python offers a gentle learning curve for new programmers as well.
> if you understand the programming model
Right, learning the syntax of a programming is easy and quick, if you already understand the programming model.
I argue that it is difficult to learn new programming models in general.
Or at least, it should be. Hence my criticism of Perl.
So you haven't seen the Malbolge language, then? http://www.lscheffer.com/malbolge.shtml
As for quick and dirty scripting in python: I do it all the time. Hell I just made a little "framework" [1] in python to do this better. I missed the while <> construct from perl for doing filters, so I made it easier to do in python. The funny thing is, many of my quick and dirty python scripts end up looking like more thought was put into them, and being more flexible, than they were ever intended to be.
[1] http://pypi.python.org/pypi?name=pyCLiFF&version=0.1&...
Rather unlikely.
http://shootout.alioth.debian.org/u32q/benchmark.php?test=al...
And most people are using 2.x which is even faster.
http://shootout.alioth.debian.org/u32q/benchmark.php?test=al...
I re-wrote the same code using python on a lark to see how bad it would be, colour me very surprised when it actually beat the original code by a considerable margin!
Are those Perl programs written to use multiple cores?
> And most people are using 2.x which is even faster.
All the 1/1 and 1 make them seem pretty much the same.
http://shootout.alioth.debian.org/u32/benchmark.php?test=all...
If you want to see Python being smoked: http://shootout.alioth.debian.org/u32q/benchmark.php?test=al...
Python, like any programming language, is not perfect. There's simply less to not be perfect.
In terms of web applications, what you can accomplish in PHP you can pretty much do in Python. But the difference comes in when you do "care" about the tools you're using.
When you want that extra power. When you want something sane to work with.
Python and friends give provide that for you.
I do love PHP, it's deployment is a snap and the frameworks we have now are kick ass. But Python is such a joy to work with it. And the longer I work with it, it gets harder to come back to PHP.
In python it would take me a bit more time and that time is not always available.
But the future is clear to me, no more PHP if I can help it. There are plenty of things that irritate me about python, but they are mostly things that you learn about, then it stays learned.
PHP keeps throwing up new ways to trip you up even after years of using it.
In a way I'm glad to have gone this route, in spite of the learning curve with python (and Django), I can already see it pay off when I see functions shrink to 1/3rd the size they were using PHP.
The only thing really still missing from the python under apache model is a multi threaded shared state environment.
That would really allow you to blow other solutions out of the water in terms of speed.
Now you still have to make that round trip to the database on the next page fetch.
Python, Ruby, Perl, Java, Erlang, allow you to do systems-programming (i.e. writing daemons, doing threading and high performance event driven/non-blocking I/O) while still retaining high-level features such as reflection/meta programming.
PHP is far from perfect, and personally I've also moved from it to Ruby and Python, but the automatic "switch from PHP to anything" on a language that powers so much of the web is meaningless.
FaceBook and I think Yahoo both make heavy use of an RPC-based, service-oriented architecture. They use PHP for the frontend but then call out to backends to do any sort of complicated processing. This is basically the only sane way to architect a large system. While I think their choice of PHP to power the front-end is regrettable (it's a major reason why I didn't apply to FaceBook), it's at least defensible.
And I'm also a PHP->Python refugee - I used PHP on a bunch of projects in college, one of which got to the 100k-to-million-user range. I would not take a PHP job unless I was desperate for cash; I hate the language that much.
I wouldn't take a PHP gig either. I would also prefer Ruby work to Python, since I enjoy it more. But that's my selfish interest as developer, it doesn't mean that for the project itself PHP isn't, sometimes, the best option for the project.
For people who want to create a web app and don't have much programming experience, PHP can still be the best choice - it's easy to deploy, it has great official and unofficial docs, it's easiest to find developers for and it's pretty reliable.
Now, this may sound crazy to many, but not only PHP has some good sides, a lot of them come from intelligent choices its designers made: for example, PHP script only runs in context of the request - no application server here - which helps reliability since memory leaking etc scripts can't cause too much harm. PHP's basically saw itself merely as an Apache module - in a rare, pre-hyposhpere display of egoless coding - making it extremely easy to deploy (compare and contrast with Rails before Passenger). The fact it comes with so many libraries welded into the common namespace is ugly for me, but a boon for beginners, and the fact you can mix PHP and HTML allowed many web designers to tinker with it, gradually moving from design to HTML to PHP - one of whom ended up writing Wordpress, I think :)
In fact, I am willing to argue that there's more reason to use something else if your app is simpler (i.e. it's a monolithic web application): you can add functionality without resorting to a more complex multi-tier/service oriented architecture or writing C-based extensions.
Yes, there are many cases where Python/Perl/Ruby can't do what Java/C++/Erlang can, but their capabilities stretch far longer than those of PHP. PHP essentially limits you to front-end web development; writing standalone scripts/tools/daemons in PHP -- been there, when a boss at a startup I worked in refused to allow me to use any other language -- is horribly painful.
There's a progression between "simple CRUD pages" and "complex, service oriented architecture". Python can fill a lot more on that continuum.
I use PHP every day, but have grown a bit tired of the lack of sophistication inherent in the language. The huge flat namespace, the inconsistent design of the standard library, the absence of advanced language features (functions aren't even a first class data member), and so on.
How is it that a minor upgrade changes functions and their behaviour so much that it breaks existing PHP5-compatible code? WTF is wrong with the PHP developers?
If I didn't have so much code (whole product with contracts to support) invested in PHP, I would switch in a heartbeat and never look back.
Do you actually know anything about languages other than PHP and want to change for legitimate reasons? Or are you just jumping on the bandwagon because it sounds cool?
It sucked trying to rework so much PHP4 code for PHP5 while keeping b/c, since that felt like the slowest community migration to a new version I've ever seen... But it seems like each additional release from PHP changes and breaks something else. My codebase is older than many, so it's possible that's made it a bit more brittle over time, but things like completely changing the allowed characters in .ini file keys from 5.2 to 5.3 I find baffling. Yes, speed things up or improve things, but consider that existing liberties you've allowed may come to be relied on by your users. If you let them do something in 5.2, make sure they can still do it in 5.3. I'm always careful myself to make sure existing code will continue to work, and new features are offered as optional or via new APIs. Code from 3.x of my software continues to run fine on 5.x as a result, which lowers my support burdens.
So again, no I'm not just fanboying for other languages or falling for the "grass is always greener" line of thinking. PHP just has certain inelegant things that I find add to my overall frustration over time. I value terseness, so things like -> versus . for objects adds up, just like $ versus nothing for variables wears at the wrists too. I value consistency, so not remembering which parameter goes first for in_array() versus strpos(), or why the differences in naming add up too. Those are minor things that have been present from the start, but then when I start getting emails from people saying my software is messing up and I find out it's just that the latest version of PHP totally broke something I'd been relying on, that frustration is amplified tenfold.
For my own projects, I've used pretty much all the fad languages from BASIC to Pascal to C. I've used industry-adopted languages like Tcl, Java and Perl. I've experimented with Lisp and Erlang.
After all is said and done, I like Python/Cython/C. It gives me the option to use a glue language for the Windows programming I've done, which is perfect. It gives me a compiled (albeit ctypes-restricted) subset language for writing hashing/storage/whatever functions. It gives me a super-awesome library set that comes built-in.
I know it'll go away someday, but I'm glad to have worked in Python for as long as I have.
Aside from the obvious that just about everything will go away someday, is there a reason you believe this?
From my vantage point (albiet limited) python looks like it has staying power. It is easy to use, effecient enough (and with unladen swallow in development it may get a lot more effecient soon), and currently seems to be gaining ground instead of losing. Eventually I am sure that it and any other language you can possibly name will get supplanted, but I am not aware of any particular reason to expect that any time remotely soon.
I might have dated myself above by mentioning Tcl and Pascal, but I've been programming for a few decades. Without ruining the cosmic punchline: all things go away surprisingly fast.
http://en.wikipedia.org/wiki/Anicca
python looks like it has staying power
So did the Roman Empire, JOVIAL, FORTH, and PL/1.
The Roman Empire lasted for many centuries, Jovial, Forth, and PL/1 all had very long runs. In fact, they are all still used in some limited capacities today.
It's difficult to be obscure in Python. Yet if I really need to do something jinky like remapping an outside library's methods, I can do it with a few lines of slightly-uglier code. That's a very hard balance to make.
Good design involves a lot of "no". I don't think languages are an exception.
It's occasionally difficult to be obscure with Python syntax. It's not at all difficult to be obscure with anything in Python above that level. Even then, if you do weird things with metaclasses or decorators, it's easy to be obscure with Python.
I seriously don't under stand that statement. Decorators are callables that return callables, nothing else, nothing special. Decorator Syntax is just sugar.
Decorators can be applied any time including runtime. I'm not even sure what you mean by "compile time" in regards to Python, import time?
Yes, the function being used as a decorator could just as easily be called at runtime, and that does break the similarity to macros somewhat.
> completely mask the def that they are applied to.
@decorator reassign to the name of the def. But if there are other refs to that def they do not change and are not "masked". It is no different than assignment. You know that these two are identical, right?
@foo
def bar():
pass
def bar():
pass
bar = foo(bar)
Also this is a perfectly valid decorater that doesn't mask the original func def deco(func):
# do something terrible clever here
return func
decorator syntax != decorators is my main point. But by conflating the two I can begin to understand your statement.If someone showed up with a patch tomorrow which allowed free threading without seriously breaking python, or crippling single threaded performance, or making extension modules impossible to write, I could very well see it getting in. In reality, doing this requires a serious reworking of the interpreter (ala unladen swallow) to make it even feasible. There's a lot of things the GIL actually helps, for example the ease of writing c-based extension module, unfortunately they come with a price.
The price is/was seen as an acceptable tradeoff, and I think it has served it well, even if if it disappeared tomorrow no one would shed a tear.
Also, braces aren't a feature :p
To piggyback: don't forget that CPython ≠ Python. The GIL is an implementation detail of CPython, not a feature of the language. Other Pythons -- Jython, for one -- have no GIL.
BTW, if you'd like to chat more about concurrency, I'm around all weekend (although I'm late getting to the University of Belgrano today). As my comments during your talk yesterday indicated, I think you're pretty off-base arguing that people aren't trying out interesting concurrency solutions in Python — ZODB is a (somewhat broken) STM that's been production-stable for many years, and Stackless is specifically targeted at actor-based modeling.
So I think people in the Python community have actually been way out in front on these issues. It's just that most people use Python for production work, not research, so the researchy stuff doesn't make it into the mainline.
The first language where I got to choose it was Lisp for the Sinclair QL, ~ 1985.