Inheritance question

Tzury Bar Yochay Afro.Systems at gmail.com
Tue Mar 25 08:30:59 EDT 2008


On Mar 25, 2:00 pm, John Machin <sjmac... at lexicon.net> wrote:
> 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)
sorry, it should have been:
a = Foo.getid(self)
instead of
a = Foo.getid()



More information about the Python-list mailing list