[Zope] RE: Passing namespace to Python scrict was: how to call a n external module from a Python script?
Dieter Maurer
dieter@handshake.de
Wed, 26 Sep 2001 20:02:02 +0200 (CEST)
Reif Peter writes:
> ....
> My variable is named "zone". The following Python-statement yields 1:
>
> return namespace.has_key("zone")
>
> The following calls yield an error (each one):
>
> return namespace.items()
> return namespace.keys()
> return getattr(namespace, "zone")
This is all as it should be!
"namespace" was obviously passed correctly, as shown
by the success of 'namespace.has_key("zone")'.
"namespace.items" and "namespace.keys" fail because
the DTML namespace implements the mapping API only
partially: "has_key" and subscription "[...]".
The bound variables are exposed via subscription
and not as attributes. Therefore,
'namespace["zone"]' will work while "namespace.zone"
and the equivalent 'getattr(namespace,"zone")' will
fail.
In some cases, you will want to use "namespace.getitem('zone')"
instead of "namespace['zone']".
Please read the "Name Lookup" and "DTML" sections of
<http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html>
Most of your problems and questions are addressed there.
Dieter