[Tutor] Test if name is defined?
Terry Carroll
carroll@tjc.com
Tue Feb 11 00:28:01 2003
Is there a way to test to see if a variable is defined other than with the
exception system?
I'm creating an object with several attributes based on the content of a
huge initialiation file. The file comprises a series of lines, each of
which is a tab-separated list of three fields: an identifier for the
object, the name of an attribute, and the value of the attribute. I want
to put an object into a dictionary only if either the
The easiest way for me to handle this is to create an object corresponding
to the identifier the first time I see it, and to set an attribute for
each line I see, until I see a new object identifier. At that time, I'll
want to either discard the object I just created, or else put it into a
dictionary indexed by the object ID; in any event, I'll go on to create a
new object corresponding to the new object ID I just read in.
I only want to keep the object if I read in a value for either the
attribute kGB0 or kBigFive attributes (or both) are defined.
If Python had a defined() method like Perl (sorry, it's my only point of
comparison), I'd do something like this:
if defined(current.kGB0) or defined(current.kBigFive):
dict[current.ID] = deepcopy(current)
I can't do this in Python, can I?
--
Terry Carroll | "To have this rare opportunity
Santa Clara, CA | is a rare opportunity."
carroll@tjc.com | - Houston Rockets' Yao Ming, on being named
Modell delendus est | starting center for the 2003 NBA All-Star Game