[Tutor] Leaving PHP for Python
W W
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
"mod_python" library.
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 [15]: class foo:
....: def write(self, mystr):
....: print mystr
....:
....:
In [17]: def handler(req):
....: req.write("Hello World!")
....:
....:
In [18]: x = foo()
In [19]: handler(x)
Hello World!
And then return apache.OK is returning... well, the object "OK" in the
apache class.
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
far off.
> 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:
978-0-596-51582-9)
Noah spoke at our un-conference in October, and I learned a lot, hence, my
recommendation.
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
other tasks.
Definitely check out the links others have provided, they'll be packed full
of helpful information, and I hope this helped as well.
-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081125/e782569d/attachment-0001.htm>
More information about the Tutor
mailing list