Hacker News new | ask | show | jobs
by dsl 4920 days ago
As someone who doesn't know much in the way of Python, it seems odd to me that many of the examples are blocks of code vs one line replacements for PHP functions. Is Python just more verbose?
7 comments

I know both languages well. Python is much more succinct than PHP because of array literals, list comprehensions, lambda's and better native data types with ways to navigate them (all the functional methods are there). Only PHP 5.4 with a literal array syntax is beginning to approach some of the things you can do in Python.

You think PHP is short because you have functions like xmlrpc_encode_request and the equivalent in Python is 20 lines. What you don't see if that xmlrpc_encode_request is pages and pages of C code and PHP macro's behind the scenes. That function should be in a library, not in a programming language, which is why PHP is more comparable to other web frameworks, not to other programming languages.

It IS a PHP library - just one written in C rather than in user-land PHP code. Why is this a problem? The XML-RPC extension isn't even enabled by default.
xmlrpc_encode_request on this site should probably be fixed to use xmlrpclib.dumps (or loads if you need the other direction). This has existed in Python since version 2.2.

I realize this is beside the point, but I'm willing to fix it myself.

There is xmlrpclib inlcuded per default and it does all the XML stuff, so i think you can easily get away far more readable then what is shown in the python reimplementation of the PHP function. http://docs.python.org/2/library/xmlrpclib.html#example-of-c...

It just doesn't have xmlrpc_encode_request() with the exact method signature.

P.S.: I am wondering if it is normal to have functions marked as EXPERIMENTAL with a biiiig warning sign for 11 years in the language? That really scares me. This function has been introduced in PHP 4.1.0 which was releases in 10-Dec-2001... That's just...

edit: removed rant about xmlrpc_encode_request being built-in. It's a non-default extension, apparently..

No it's not.

The reason the examples appear longer is because the website is documenting how to implement PHP functions in Python. Since Python is not PHP, it won't implement line-for-line replacement functions.

No, just PHP has a tradition of implementing frequently used code in C. This means PHP has tons of functions that serve specific use case, while Python has less of them, at least in standard package (of course you can have a module that does the same and then you'd have one-liners too). So you're not comparing the same thing here.
Using both PHP5 in the past and Python more recently, I find that most things in Python need far less code to write than PHP.

The examples here are kind of disingenuous to that, but if you start writing code that takes advantage of the features of Python, your lines of code should be less than PHP or equal.

I don't think it's complete. For example this: http://www.php2python.com/wiki/function.array-walk/ could be replaced with map() in Python.
No, there are a lot of shortcuts and beautiful one-liners in Python. However, Pythonic code always focuses on being the simplest and cleanest as opposed to being the shortest. I suspect it's the author's way of imposing some good python coding standards at the same time.