Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Polymorphism the Python way

Reply
Thread Tools

Re: Polymorphism the Python way

 
 
Graham Fawcett
Guest
Posts: n/a
 
      08-31-2003
Daniel Klein wrote:

>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.
>
>Thanks,
>Daniel Klein
>
>


Why not just use the same attribute name in each class?

class Dead(object):
def __init__(self, deadthing):
self.thing = deadthing

class Parrot(object):
def __init__(self, parrotthing):
self.thing = parrotthing

Then you can access the attribute as `someobj.thing`.

Given that you hate getters, and that you didn't title your message,
"Encapsulation the Python Way", accessing instance attributes directly
is fair game.

-- Graham



 
Reply With Quote
 
 
 
 
Daniel Klein
Guest
Posts: n/a
 
      09-02-2003
On Sun, 31 Aug 2003 03:47:49 -0400, Graham Fawcett
<(E-Mail Removed)> wrote:


>Why not just use the same attribute name in each class?
>
> class Dead(object):
> def __init__(self, deadthing):
> self.thing = deadthing
>
> class Parrot(object):
> def __init__(self, parrotthing):
> self.thing = parrotthing
>
>Then you can access the attribute as `someobj.thing`.
>
>Given that you hate getters, and that you didn't title your message,
>"Encapsulation the Python Way", accessing instance attributes directly
>is fair game.


Yes, I could do this, but a method provides better control over the
state.

Daniel Klein
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
polymorphism and/or named parameters: the ruby way? Stephan Mueller Ruby 7 08-07-2005 09:05 AM
polymorphism in python rashkatsa Python 7 11-26-2003 11:50 PM
Re: Polymorphism the Python way Michael Peuser Python 6 09-03-2003 05:38 PM
Re: Polymorphism the Python way Peter Otten Python 1 09-02-2003 01:03 PM



Advertisments