How to know when a variable is set

Chris Liechti cliechti at gmx.net
Sat Nov 17 18:38:55 EST 2001


[posted and mailed]

madsurfer2000 at hotmail.com (-) wrote in
news:fef0a228.0111171234.6f4b19e7 at posting.google.com: 

> Michael Hudson <mwh at python.net> wrote in message
> news:<uvggdl5ek.fsf at python.net>... 
>> madsurfer2000 at hotmail.com (-) writes:
> I like the idea of structs, and I was planning to use a class the same
> way, like
> 
> class info:
>     pass
> ...
> info.name = ... 
> 
> 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.

i usualy initialize undefined attributes with "None" and later i can test 
if it's still "None" or a real value.
its easier to understand how a class works when all attributes exist from 
the beginning (__init__). it's more complicated to see that an attribute is 
created dynamicaly.

class Info:
    	def __init__(self):
    	    	self.name = None

    	def check(self):
    	    	if self.name is None:
    	    	    	print "Name is not set"

i = Info()
i.check()
i.name = 'hello'
i.check()

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list