Inheritance question

Gerard Flanagan grflanagan at gmail.com
Tue Mar 25 12:36:49 EDT 2008


On Mar 25, 4:37 pm, Brian Lane <b... at brianlane.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
>
> Gerard Flanagan wrote:
> > Use the child class when calling super:
>
> > --------------------------------------
> > class Foo(object):
> >     def __init__(self):
> >         self.id = 1
>
> >     def getid(self):
> >         return self.id
>
> > class FooSon(Foo):
> >     def __init__(self):
> >         Foo.__init__(self)
> >         self.id = 2
>
> >     def getid(self):
> >         a = super(FooSon, self).getid()
> >         b = self.id
> >         return '%d.%d' % (a,b)
>
> > print FooSon().getid()
> > --------------------------------------
>
> That still doesn't do what he's trying to do.
>

Ah, I see. Maybe something like the following then?

--------------------------------------
class Foo(object):
    def __init__(self):
        self._id = [1]

    def getid(self):
        return '.'.join(str(i) for i in self._id)

class FooSon(Foo):
    def __init__(self):
        Foo.__init__(self)
        self._id.append(2)

print FooSon().getid()
--------------------------------------

G.



More information about the Python-list mailing list