How to know when a variable is set
Michael Hudson
mwh at python.net
Mon Nov 19 06:50:54 EST 2001
madsurfer2000 at hotmail.com (-) writes:
> Michael Hudson <mwh at python.net> wrote in message news:<uvggdl5ek.fsf at python.net>...
> > madsurfer2000 at hotmail.com (-) writes:
> >
> > > How can I test if a variable has been given a value, or if it's
> > > undefined?
> >
> > Why would you want to do that? Specificaly, in what context do you
> > want to know?
> >
>
> I like the idea of structs, and I was planning to use a class the same
> way, like
>
> class info:
> pass
> ...
> info.name = ...
Oh. In this case you'd want hasattr(). Or maybe try:/ except
AttributeError:.
> These would be set in an event driven GUI, and I needed to know if
> an attribute had been set before I saved the information. Now that I
> have thought more about it, an hash table would be a better choice.
Well, the above is only syntax sugar for a hash-table, as it happens.
But you're probably right.
Cheers,
M.
--
In many ways, it's a dull language, borrowing solid old concepts
from many other languages & styles: boring syntax, unsurprising
semantics, few automatic coercions, etc etc. But that's one of
the things I like about it. -- Tim Peters, 16 Sep 93
More information about the Python-list
mailing list