Hi, How can we in the python script check if the a certain variable exists in the namespace? ie. We can get the value of a variable by using context.myvar or context['myvar'], but both will return an error when the myvar doesn't exists. Now, how can you check if it exists? I tried several things, like context.hasattr('myvar') or context.has_key('myvar') , _hasattr(), _.hasattr() ... etc, but none seem to work. Can somebody tell me if it even is possible? Thanks. Tom.
What was wrong with if hasattr(context,'coffeebreak'): print "have coffee" ? This is almost the equivalent of DTML's <dtml-if coffeebreak> Assuming your completely skip the REQUEST object out there. It doesn't have to be 'context' like above or 'self' in External Method. You can even do this one any Zope object. if has(context.folder['document.html'], 'coffeebreak'): print "had coffee" ----- Original Message ----- From: "Tom Deprez" <tom.deprez@uz.kuleuven.ac.be> To: <zope@zope.org> Sent: Monday, May 28, 2001 11:38 AM Subject: [Zope] Python Scripts
Hi,
How can we in the python script check if the a certain variable exists in the namespace?
ie. We can get the value of a variable by using context.myvar or context['myvar'], but both will return an error when the myvar doesn't exists. Now, how can you check if it exists?
I tried several things, like context.hasattr('myvar') or context.has_key('myvar') , _hasattr(), _.hasattr() ... etc, but none seem to work. Can somebody tell me if it even is possible? Thanks.
Tom.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
euhm, nothing... if I look back on the script I tried out I had written hassattr .... Urgh, typos, why do they exist? Anyway, thanks for the response! Tom. ----- Original Message ----- From: "Peter Bengtsson" <mail@peterbe.com> To: "Tom Deprez" <tom.deprez@uz.kuleuven.ac.be>; <zope@zope.org> Sent: Monday, May 28, 2001 12:15 PM Subject: Re: [Zope] Python Scripts
What was wrong with if hasattr(context,'coffeebreak'): print "have coffee" ?
This is almost the equivalent of DTML's <dtml-if coffeebreak>
Assuming your completely skip the REQUEST object out there.
It doesn't have to be 'context' like above or 'self' in External Method. You can even do this one any Zope object. if has(context.folder['document.html'], 'coffeebreak'): print "had coffee"
----- Original Message ----- From: "Tom Deprez" <tom.deprez@uz.kuleuven.ac.be> To: <zope@zope.org> Sent: Monday, May 28, 2001 11:38 AM Subject: [Zope] Python Scripts
Hi,
How can we in the python script check if the a certain variable exists in the namespace?
ie. We can get the value of a variable by using context.myvar or context['myvar'], but both will return an error when the myvar doesn't exists. Now, how can you check if it exists?
I tried several things, like context.hasattr('myvar') or context.has_key('myvar') , _hasattr(), _.hasattr() ... etc, but none seem to work. Can somebody tell me if it even is possible? Thanks.
Tom.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
What was wrong with if hasattr(context,'coffeebreak'): print "have coffee" ?
This is almost the equivalent of DTML's <dtml-if coffeebreak>
Not really... the DTML equivalent would be: <dtml-if "hasattr(context,'coffeebreak')"> ...which is something quite different ;-) In a python script, bind the namespace to _ on the bindings tab and then see what happens with: if _['coffeebreak']: print 'coffee' cheers, Chris
What was wrong with if hasattr(context,'coffeebreak'): print "have coffee" ?
This is almost the equivalent of DTML's <dtml-if coffeebreak>
Not really... the DTML equivalent would be: <dtml-if "hasattr(context,'coffeebreak')"> ...which is something quite different ;-)
In a python script, bind the namespace to _ on the bindings tab and then see what happens with:
if _['coffeebreak']: print 'coffee'
The purpose of the Script bit is to check for the presence of an object OR property, isn't it?
cheers,
Chris
if _['coffeebreak']: print 'coffee'
The purpose of the Script bit is to check for the presence of an object OR property, isn't it?
Sorry, you've lost me...
<copy original>
How can we in the python script check if the a certain variable exists in the namespace?
<myresponse> use if hasattr(context,'variable'): <restoftheconfusion> I'm lost too
Chris
participants (3)
-
Chris Withers -
Peter Bengtsson -
Tom Deprez