Inheritance question

Anthony anthonysmith80 at gmail.com
Tue Mar 25 11:31:49 EDT 2008


On Mar 25, 2:31 pm, Tzury Bar Yochay <Afro.Syst... at gmail.com> wrote:
> I wish it was that simple but 'a = Foo().getid()' is actually creating
> a new instance of Foo whereas I want the data of the Foo instanced by
> __init__ of FooSon().

I don't think Foo.__init__(self) creates an instance of the Foo
class.  If you want FooSon to create an instance of Foo, try:

class FooSon(Foo):
    def __init__(self):
        self.foo = Foo()
        self.id = 2
    def getid(self):
        a = self.foo.getid()
        b = self.id
        return '%d.%d' % (a,b)

>>> FooSon().getid()
'1.2'

Or change "self.id" in Foo to something else, e.g., "self.id_foo".

Does that help?

Anthony



More information about the Python-list mailing list