design question: no new attributes

greg greg at cosc.canterbury.ac.nz
Thu Mar 1 10:11:35 CET 2007


Alan Isaac wrote:

> class NothingNew:
>     a = 0
>     def __init__(self):
>         self.b = 1
>         self.initialized = True

There's a problem with that when you want to subclass:

   class NothingElseNew(NothingNew):

     def __init__(self):
       NothingNew.__init__(self)
       self.c = 42 # <--- Not allowed!

You could get around this by temporarily de-initializing
it, i.e.

     def __init__(self):
       NothingNew.__init__(self)
       del self.__dict__['initialized']
       self.c = 42
       self.initialized = True

but that's not very elegant.

--
Greg



More information about the Python-list mailing list