page contents are not refreshed
waylan
waylan at gmail.com
Wed Sep 13 15:47:18 EDT 2006
Steve Holden wrote:
> waylan wrote:
[snip]
> >
> >>from mod_python import apache
> >>from time import strftime, gmtime
> >>
> >
> > def curtime():
> > return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
> >
> >
> >>def handler(req):
> >> req.content_type = "text/plain"
> >> req.send_http_header()
> >> req.write(str(curtime()))
> >> return apache.OK
> >
> >
> This is a very long way round for a shortcut (though it does have the
> merit of working). Why not just
>
> def handler(req):
> req.content_type = "text/plain"
> req.send_http_header()
> curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
> req.write(str(curtime))
> return apache.OK
>
> Or even
>
> def handler(req):
> req.content_type = "text/plain"
> req.send_http_header()
> req.write(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
> return apache.OK
>
While Steve's examples certainly do the trick in this limited case, I
assumed that the original poster was just starting with mod_python and
I was simply trying to explain the bigger picture for future reference.
As one develops more sophisticated code, simply adding it to the
`handler` function becomes less desirable. Reacognizing that anything
that must be reevaluated on each request must be callable will be a
bigger help IMHO.
Steve's examples work because the current time is evaluated within
`handler` and :
>>> callable(handler)
True
While in the the original example:
>>> callable(curtime)
False
Yet in my example:
>>> callable(curtime)
True
Finally, by way of explaination:
>>> callable.__doc__
'callable(object) -> bool\n\nReturn whether the object is callable
(i.e., some kind of function).\nNote that classes are callable, as are
instances with a __call__() method.'
More information about the Python-list
mailing list