any ways to judge whether an object is initilized or not in a class

Steve Holden steve at holdenweb.com
Mon Mar 19 13:19:50 EDT 2007


Bruno Desthuilliers wrote:
> Diez B. Roggisch a écrit :
>> momobear schrieb:
>>> hi, I am puzzled about how to determine whether an object is
>>> initilized in one class, anyone could give me any instructions?
>>> here is an example code:
>>>
>>> class coffee:
>>>          def  boil(self):
>>>                self.temp = 80
>>>
>>> a = coffer()
>>> if a.temp > 60:
>>>      print "it's boiled"
>>>
>>> in C++ language we must initilized a variable first, so there is no
>>> such problem, but in python if we don't invoke a.boil(), we will not
>>> get self.temp to be initilized, any way to determine if it's initilzed
>>> before self.temp be used.
>> You want boil to be called __init__, which is python's constructor name. 
> 
> <nitpicking>
> Actually, __init__ is the initializer. The proper constructor is __new__.
> </nitpicking>
> 
<nitpicking severity="be-kind-to-novices">
Actually you would have to ensure that the class's metaclass was <type 
"type"> for that to be true. Classic classes don't *have* a __new__ method.
</nitpicking>

And of course the real answer is that the __init__ method should set a 
default value (modulo the sneaky use of class attributes) for any 
attributes that might be accessed before other methods get a chance to 
set them.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list