initializing mutable class attributes

Peter Otten __peter__ at web.de
Mon Aug 30 10:35:03 EDT 2004


Dan Perl wrote:

> something like this:
>     class Child (Father):
>         def __init__(self):
>             Father.__init__(self)
>             self.attr3=[ ]
> 
> I find this even more awkward because many people will forget to do it.

You have to call base-class-__init__() in every non-trivial inheritance
scheme. Should you forget it, pychecker is always there to remind you

<sloppy-code>
class X:
    def __init__(self):
        self.x = 2

class Y(X):
    pass

class Z(Y):
    def __init__(self):
        pass
</sloppy-code> 

And here's what pychecker says:

$ pychecker t1.py
Processing t1...

Warnings...

t1.py:9: Base class (t1.Y) __init__() not called

Clear enough. 

Peter




More information about the Python-list mailing list