CGI with Python: advantages?
Alex Martelli
alex at magenta.com
Sun Aug 6 18:19:10 EDT 2000
"Paul Schreiber" <paul at magic.ca> wrote in message
news:paul-2AFA6F.12063306082000 at news.telocity.net...
> I missed the beginning of this thread, but I'll jump in anyway.
>
> One thing that drives me nuts about Python is that you can't reference
> undefined variables or undefined keys in a dictionary.
But that's so trivially easy to fix, if you want -- e.g.:
class cgi_defaulter:
def __init__(self):
self.form=cgi.FieldStorage()
def __getattr__(self,name):
try:
return self.form[name].value
except KeyError:
return ''
x=cgi_defaulter()
> The reason this is an issue is that web forms don't always have all the
> fields submitted, and this requires extra work in python.
The above 10 lines at the start of the script -- that's all. And it's
easy to put the class definition into a mycgi.py module that imports cgi,
and just import it and instantiate the class into x -- that's down to 2
lines' worth of overhead.
> if form.has_key('checked'):
> checked = form['checked'].value
>
> versus:
> $checked
x.checked
doesn't seem any worse to me than $checked -- on the contrary.
> I prefer PHP for doing web pages. It's designed explicitly for that
> task, and it's much faster since it's compiled into Apache. And it has
mod_python or mod_snake offer you comparable speed, and FastCGI lets
you get very similar speed while keeping really close to a cgi model
of programming. And the fact that a language "is designed explicitly"
for a specific task doesn't necessarily means it's _better_ for it; I
do not know PHP enough to tell whether this issue applies to it, but
I do know that Python's general brilliance is hard indeed to match
(if a certain language semantic aspect isn't what you want for a given
task, it's often easy to tweak it -- just use a class, and the various
special methods, such as __getattr__ here, and voila).
Alex
More information about the Python-list
mailing list