[Zope-dev] Re: Arbitrary Arguments?
Tres Seaver
tseaver at zope.com
Sun Jun 6 22:14:47 EDT 2004
Marshall Powers wrote:
> In Python, you can declare a function to accept an arbitrary number of
> arguments by adding an argument that starts with * or **. A *-prefixed
> argument gets a tuple of the non-keyworded arguments that follow the
> declared arguments, a **-prefixed arguments gets a dictionary of
> keyword:value pairs of the keyworded arguments not declared in the
> argument list preceding it. For example,
>
> def foo(a, b=2, *stuff, **morestuff):
> for x in stuff:
> print x,
>
> print
> for k in morestuff.keys():
> print k, morestuff[k]
>
> if i call
> foo(1, 2, 3, 4, 5, 6, x='a', y='b', z='c')
>
> foo will print
> 4 5 6
> x a
> y b
> z c
>
> My problem is that when I write a Script(Python) in zope, and declare a
> *- or **-prefixed argument to my script, I always get an empty tuple or
> dictionary. It's not that im using an undeclared argument or anything,
> otherwise my script would get a runtime error. It just that the tuple
> or dictionary is not given any values when I make a call with 'extra'
> arguments.
>
> Does zope just not support this python feature? Should I just parse the
> arguments myself from the URL?
If you call your PythonScript from another script using keywords or
positional arguments, you will find that the 'stuff' and 'morestuff'
arguments are filled as you expect. However, when you invoke the script
directly via a GET or POST request, the publisher has no way to know
which of the myriad request variables to pass, and so it punts.
If you want to get arbitrary access, you can add REQUEST as an argument,
and then use its 'form' subobject to get the form / query string values
out. E.g.:
##Script (Python) "foo"
##parameters=a, b=2, *stuff, **morestuff, REQUEST=None
if REQUEST is not None:
morestuf.update(REQUEST.form)
# ...
Tres.
--
===============================================================
Tres Seaver tseaver at zope.com
Zope Corporation "Zope Dealers" http://www.zope.com
More information about the Zope-Dev
mailing list