[Tutor] Leaving PHP for Python
srilyk at gmail.com
Tue Nov 25 15:39:08 CET 2008
On Tue, Nov 25, 2008 at 6:43 AM, Jason DeBord <jasdebord at gmail.com> wrote:
> The following for example:
> from mod_python import apache
> def handler(req):
> req.write("Hello World!")
> return apache.OK
> Frankly, I don't understand what is going on in the above. This is a bit
> different compared to what I am used to.
I don't know if you did much OOP (Object Oriented Programming) in PHP, or
anything else, but that's what python does, and does well. There are some
tutorials out there that explain what objects really are... but I read
somewhere that in python /everything/ is an object, and that's pretty much
true to my experience.
The line starting with from is similar to an include statement in php.
Though what you're doing is including "apache" that happens to be inside the
The next line:
def means you're defining a function (or method, if it's in a class, where
it would be def handler(self, req), but that's another story)
handler is the name you're calling the function and req is the name of your
argument. At this point, it really doesn't matter what req is... it's just a
name that will point to (or become a copy of) whatever you pass to it.
In this case, you're passing it a class that has the method "write".
Consider this example(I'm using the IPython active interpreter, so you see
In instead of >>>):
In : class foo:
....: def write(self, mystr):
....: print mystr
In : def handler(req):
....: req.write("Hello World!")
In : x = foo()
In : handler(x)
And then return apache.OK is returning... well, the object "OK" in the
Of course, I've never used mod_python/apache, that's just me applying what I
know about python, so I may not be 100% accurate, but I don't think I'm too
> So, my question, would you all please point me to some introductory
> resources, tutorials, books, that focus on Python programming for the web? I
> am eventually going to interface with some web services, notably Amazon Web
> Services. Also, I'd like to write some server side scripts to serve as a
> backend to some Adobe AIR apps.
If you plan on doing much command line/admin type stuff, I'd recommend
"Python for Unix and Linux System Adminstration" by Noah Gift & Jeremy M.
Jones, available through O'Reilly, at least as one resource (ISBN:
Noah spoke at our un-conference in October, and I learned a lot, hence, my
It does a great job of throwing you into a lot of various administration
tasks, which instruction can be applied to (and is also useful for) many
Definitely check out the links others have provided, they'll be packed full
of helpful information, and I hope this helped as well.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor