Inheritance question

Brian Lane bcl at brianlane.com
Tue Mar 25 11:37:00 EDT 2008


-----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.

The problem here is that you only have one instance. self refers to it,
so when you set self.id in the super you have set the instance's id to
1. When you set it to 2 after calling the super's __init__ you are now
setting the *same* variable to a 2.

Basically, you can't do what you are trying to do without using a
different variable, or keeping track of a separate instance for the
super instead of sub-classing it.

Brian

- --
- ---[Office 71.7F]--[Outside 33.2F]--[Server 99.5F]--[Coaster 68.7F]---
- ---[      KLAHOWYA WSF (366773110) @ 47 31.2076 -122 27.2249      ]---
Software, Linux, Microcontrollers             http://www.brianlane.com
AIS Parser SDK                                http://www.aisparser.com
Movie Landmarks Search Engine            http://www.movielandmarks.com

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Remember Lexington Green!

iD8DBQFH6RwcIftj/pcSws0RArdpAJ9pCMjrkpBarHyQsl6hXEifj52giwCfXfKa
Ot/1a+NNOLN5LA4mxBtfpis=
=Yacu
-----END PGP SIGNATURE-----



More information about the Python-list mailing list