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

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Mar 19 09:08:29 EDT 2007


momobear a écrit :
> 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,

Obviously. This is why it's good form to set all public attributes in 
the initializer method:

class Coffee(object):
   def __init__(self):
     self.temp = 20
   def boil(self):
     self.temp = 80




More information about the Python-list mailing list