initializing mutable class attributes

David Bolen db3l at fitlinxx.com
Thu Sep 2 02:01:45 CEST 2004


"Dan Perl" <dperl at rogers.com> writes:

> After all this discussion, what do people think about this code?

On face value, I'd question why you're going through the extra effort.
I may be missing the point in such a small example, but just having an
__init__ in the base class that subclasses are supposed to call seems
simpler.

If you're just trying to find some way to have default attributes that can
be used from subclasses without calling __init__, I'd probably just use
class level definitions.  For example:

          - - - - - - - - - - - - - - - - - - - - - - - - -
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Test(object):
...     attr1 = 666
... 
>>> class Derived(Test):
...     def __init__(self):
...         self.attr2 = 111
... 
>>> d = Derived()
>>> print d.attr1, d.attr2
666 111
>>> print isinstance(d, Test)
True
>>> 
          - - - - - - - - - - - - - - - - - - - - - - - - -

-- David



More information about the Python-list mailing list