Polymorphism the Python way

Michael Peuser mpeuser at web.de
Sun Aug 31 09:37:00 CEST 2003


"Daniel Klein" <danielk at aracnet.com> schrieb im Newsbeitrag
news:smr2lvop984g1k1q30iidmm0lg8uecolqa at 4ax.com...
> Given the following code,,,
>
> class Dead(object):
>     def __init__(self, adeadthing):
>         self.deadthing = adeadthing
>
>     def getthing(self):
>         return self.deadthing
>
> class Parrot(object):
>     def __init__(self, aparrotthing):
>         self.parrotthing = aparrotthing
>
>     def getthing(self):
>         return self.parrotthing
>
> ...and then somewhere in some script I use...
>
>     self.getthing()
>
> to get whatever 'thing' I want to get.
>
> Isn't there a better way to do this in Python? I hate doing these
> 'get' type methods. The seem ugly to me.

I am not sure I understand your problem. What you do is fine (though it is
not exactly 'polymorphism'). I assume you  mean:
       instance.getthing()
because writing self.getthing() has unclear connotations.
Of couzrse you can call your attributes also 'thing' (instead of
'parrotthing' and 'deadthing') and you can use simply
     instance.thing
There are pros and cons.
But as I stated I am not quite sure what your problem is in the first
place....

Kindly
Michael P







More information about the Python-list mailing list