Syntactic pain.. :P

Fredrik Lundh fredrik at pythonware.com
Mon Sep 6 10:46:30 EDT 1999


Markus Stenberg <mstenber at cc.Helsinki.FI> wrote:
> As far as I've read, there isn't any way to make this any more .. elegant:
> 
> class Foo:
> def __init__(self, a1, a2, a3="x", a4="y"): pass
> 
> class Bar(Foo):
> def __init__(self, *args, **kwargs):
> apply(Foo.__init__, tuple([self]+list(args)), kwargs)

apply(Foo.__init__, (self,) + args, kwargs)

> Ok, as far as I'm concerned, the initial Foo definition looks clear. Bar
> definition looks fine and dandy too, except for the black apply magic. Is
> there no more .. hm. novice-readable way, to do subclasses with just
> slightly different constructors? (I'm curious about this because although
> _I_ know Python fairly well, some people who might need to read my code
> might not, and the meaning of that construct is not quite apparent at first
> glance)
> 

here's one way to do it:

    class Foo:

        def __init__(self, a1, a2, ...):
            self.a1 = a1
            ...
            self.setup()

        def setup(self):
            pass

    class Bar(Foo):

        def setup(self):
            ...
            Foo.setup(self)
            ...

</F>





More information about the Python-list mailing list