Newbie: is a variable defined?

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Dec 20 15:26:49 EST 2002


>>>>> "Christopher" == Christopher Swingley <cswingle at iarc.uaf.edu> writes:

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

A standard idiom for this is 

  try: efrom
  except AttributeError: select_default(with_default)
  else: select_using(efrom)

It sometimes is useful in variants of this approach to be able to
delete efrom after using it, in which case you can use 'del efrom'

Many prefer the above to the look before you leap approach:

  efrom = None
  efrom = try_to_get_efrom()

  if efrom is not None: do_something(efrom)
  else: do_something_else()  


John Hunter




More information about the Python-list mailing list