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

momobear wgwigw at gmail.com
Mon Mar 19 04:35:00 EDT 2007


On Mar 19, 4:19 pm, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
> 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.
> Then it will be called in a statement like
>
> a = coffee()
>
> automatically.
>
> Diez

sorry, I should add more code to implement my ideas.
class coffee:
         def __init__(self):
              '''
              do something here
              '''
         def  boil(self):
               self.temp = 80

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




More information about the Python-list mailing list