Inheritance question

John Machin sjmachin at lexicon.net
Tue Mar 25 13:00:08 CET 2008


On Mar 25, 10:44 pm, Tzury Bar Yochay <Afro.Syst... at gmail.com> wrote:
> given two classes:
>
> 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 = Foo.getid()
>         b = self.id
>         return '%d.%d' % (a,b)
>
> While my intention is to get 1.2 I get 2.2
> I would like to know what would be the right way to yield the expected
> results

Post the code that you actually executed. What you have shown lacks
the code to execute it ... but the execution will fail anyway:

    a = Foo.getid()
TypeError: unbound method getid() must be called with Foo instance as
first argument (got nothing instead)



More information about the Python-list mailing list