Testing existance of variable

Alex the_brain at mit.edu
Sun Oct 15 08:15:06 EDT 2000


> if os.environ.has_key('REMOTE_HOST') or os.environ.has_key('REMOTE_ADDR'):
> 	  remote_id = os.environ['REMOTE_HOST'] or os.environ['REMOTE_ADDR']
> 
> However, this fails with an exception for KeyError if one of them
> doesn't exist.

Try this:

new_remote_id = os.environ.get('REMOTE_HOST') or \
                os.environ.get('REMOTE_ADDR')
if new_remote_id:
    remote_id = new_remote_id

Alex.

-- 
Speak softly but carry a big carrot.




More information about the Python-list mailing list