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

Ben Finney bignose+hates-spam at
Mon Mar 19 09:48:37 CET 2007

"momobear" <wgwigw at> writes:

> class coffee:
>          def __init__(self):
>               '''
>               do something here
>               '''
>          def  boil(self):
>                self.temp = 80
> a = coffer()
> if a.temp > 60:
>      print "it's boiled"

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

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

In Python, it's conventional to name classes in TitleCase, and
instances in lower_case.

It's also best to inherit every class from another class, leading to a
single hierarchy for all classes and types. 'object' is the one to
choose if you don't want the behaviour of any other class.

As for the original question: the __init__ method of a class is called
immediately after the constructor, so that's the place to initialise
any instance attributes.

 \      "At my lemonade stand I used to give the first glass away free |
  `\          and charge five dollars for the second glass. The refill |
_o__)                         contained the antidote."  -- Emo Philips |
Ben Finney

More information about the Python-list mailing list