On Wed, Jul 24, 2002 at 09:09:55AM -0400, Ed Leafe wrote:
<alert type="newbie">
How do I test if a value exists in Python? I'd really like to avoid using try/except if possible. I need to do something like:
if defined(x): print 'x is cool' else: print 'x does not exist'
</alert>
This kind of depends on what 'x' is. If it should be in the REQUEST then you can do something like if REQUEST.has_key(x): #do stuff else: #do some other stuff If x is just a python script variable then it kind of depends on how your script is set up. If it is being passed in as a parameter then you can do: if x: #do stuff else: #do other stuff This will basically check to see if x is None or an empty string "" or 0. If you are actually defining x in your script then you can also use the above code, but only after you define x. If x hasn't been defined and you try: if x: #do stuff you will get an error. If you are not sure when x will be defined, then you may have to use a try/except block. HTH, Chris
___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://foxcentral.net
_______________________________________________ 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 )
-- Chris Meyers Huttleston Data Design 7941 Tree Lane Suite 200 Madison WI 53717