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

Duncan Booth duncan.booth at invalid.invalid
Mon Mar 19 06:06:09 EDT 2007


"momobear" <wgwigw at gmail.com> wrote:

> thanks for help:), I am puzzled about if I have to use try and except
> to determine it. finnal code should like this?
>  class coffee:
>          def __init__(self):
>               '''
>               do something here
>               '''
>          def  boil(self):
>                self.temp = 80
> 
> a = coffer()
> try:
>     if a.temp > 60:
>         print "it's boiled"
> except AttributeError:
>     print "it's not boiled"

No, you should simply do the first of the options I suggested. i.e. 
initialise the value

class coffee:
   def __init__(self):
    	self.temp = 20

   def boil(self):
      self.temp = 80

a = coffee()
if a.temp > 60:
   print "it's boiled"
else:
   print "it's not boiled"



More information about the Python-list mailing list