Migrating from PHP to Python...

Ignacio Vazquez-Abrams ignacio at openservices.net
Sun Sep 2 19:27:30 EDT 2001


On Sun, 2 Sep 2001, Rick Shin wrote:

> Hi,
>
> I'm a PHP man looking to migrate to Python for a change of scenery. I've
> skimmed over the specs, but missed a few PHP constructs that I find
> essential. Can anyone tell me the Python equivalents for:
>
> 1. include()/require()  (is the import function meant for all file
> includes?)
> 2. header()  (for redirecting)
> 3. variable variables  (or dynamic variables)
> 4. PHPlib's template.inc  (this isn't a construct, but a templating class I
> find indispensable)
>
> Finally, as someone who develops web apps exclusively, can anyone give me
> any compelling reasons why I should learn Python? From what I've read, PHP
> is faster, less resource intensive, and more prevalent (especially with web
> hosts). I've had, on the other hand, no formal training but recently read
> Code Complete, and wish to transcend the ranks of hackers. Python seems like
> an ideal language for instilling good, modular programming practices. Plus,
> it sounds cool. I like the idea of telling people "I program Python!".
>
> Thanks for reading this,
>
> Rick

I work in both PHP and Python, so here are my answers:

1) The import _statement_ is for adding additional Python modules to the
namespace that the statement appears in. For the equivalent of include()ing or
require()ing non-code files, you'll have to use the readlines() method of the
file object or the fileinput module or the like.

2) You'll have to print the header manually before sending the output. Big
deal; excluding output buffering that's the same way that PHP does it.

3) I have yet to find a need for them in Python, although you could do
soemthing with getattr(). Somebody else?

4) Hmm. Never used it. You could always use HTMLgen.py, string formatting, and
dictionaries in tandem to simulate it I suppose.

Don't think of PHP and Python as two distinct items. Although there is some
overlap viz. cgi.py and PHP-GTK, it's best to think of PHP and Python as two
complementary halves of a single whole programming library.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list