<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> ___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://foxcentral.net
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
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>
i use the locals() builtin -- it returns a dictionary containing the current symbol table, you can use something like locals().has_key('var') this isnt allowed in pythonscripts though. why the aversion against try/except (just curious)? - peter.
On Wednesday, July 24, 2002, at 09:54 AM, peter sabaini wrote:
why the aversion against try/except (just curious)?
My general coding standards always prefer to make an explicit test rather than catching errors. I think the code is more readable, and is more in line with the thought process that created it. If there is a way to test something, I strongly prefer to use that, and only resort to catching errors if necessary. ___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://foxcentral.net
Ed Leafe wrote:
On Wednesday, July 24, 2002, at 09:54 AM, peter sabaini wrote:
why the aversion against try/except (just curious)?
My general coding standards always prefer to make an explicit test rather than catching errors. I think the code is more readable, and is more in line with the thought process that created it. If there is a way to test something, I strongly prefer to use that, and only resort to catching errors if necessary.
i also think that explicit testing is a Good Thing(tm) but one could argue that the absence of a variable is "sufficiently erroneous" to warrant a try/except block... oh well, i guess i'm getting offtopic :-) cheers, peter.
participants (3)
-
Chris Meyers -
Ed Leafe -
peter sabaini