Newbie: is a variable defined?

Christopher Swingley cswingle at iarc.uaf.edu
Fri Dec 20 14:26:25 EST 2002


Greetings,

Short version: I'm wondering how you test variables to see if they've 
been defined or not.

Longer version: What I'm trying to do is parse a file that may or may 
not contain an email address.  If it does, I use regular expressions to 
extract the username portion of the email address and place this in a 
variable named 'efrom'.  Later, I want to search a SQL database, and if 
'efrom' has been defined it will perform one SELECT and if it's not 
it'll do something else.

I could set up a seperate flag 'efrom_defined = 0', update it when efrom 
gets a value, and then test this flag.  Or I could use a 'try: except 
NameError:' block.  Or I could do 'efrom = ""' at the beginning of the 
program and then test 'if len(efrom):'

How would you do this?

Thanks.

Chris
-- 
Christopher S. Swingley           email: cswingle at iarc.uaf.edu
IARC -- Frontier Program          Please use encryption.  GPG key at:
University of Alaska Fairbanks    www.frontier.iarc.uaf.edu/~cswingle





More information about the Python-list mailing list