Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Polymorphism the Python way

 
 
Michael Peuser
Guest
Posts: n/a
 
      08-31-2003

"Daniel Klein" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Daniel Klein
Guest
Posts: n/a
 
      09-02-2003
On Sun, 31 Aug 2003 09:37:00 +0200, "Michael Peuser" <(E-Mail Removed)>
wrote:

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


Yes, sorry...'instance' (or 'object') is what I meant.

It's not a problem really. I just thought there would be a better,
more elegant way to achieve polymorphism without using getters.

Daniel Klein
 
Reply With Quote
 
 
 
 
Bertel Lund Hansen
Guest
Posts: n/a
 
      09-02-2003
Daniel Klein skrev:

>It's not a problem really. I just thought there would be a better,
>more elegant way to achieve polymorphism without using getters.


A note on words:

If a class operates with for instance a bullet, then derived
classes thereof may operate with a metal bullet, a rubber bullet,
a grenade or a missile. They all operate as bullets, and the fact
that the same object can be instantiated as many different
things, is called polymorphism. "Poly morph" = "many shaped".

"Encapsulation" is the word that describes the effort(s) to make
it impossible for the user of a module to access the attributes
(and hidden methods) directly and only allow him to do it through
public methods with build-in check so that no disasters occur.

--
Bertel, Denmark
 
Reply With Quote
 
Alan Gauld
Guest
Posts: n/a
 
      09-02-2003
On Tue, 02 Sep 2003 16:34:37 +0200, Bertel Lund Hansen
<(E-Mail Removed)> wrote:
> "Encapsulation" is the word that describes the effort(s) to make
> it impossible for the user of a module to access the attributes
> (and hidden methods) directly and only allow him to do it through
> public methods with build-in check so that no disasters occur.


<RANT>
Does anyone else really hate this corruption as much as me?
It seemed to take hold when C++ appeared. Encapsulation in OO
originally meant the ability to bind data and functions together
into a single thing (that we call an object). (Some early "OO"
languages apparently provided objects without an explicit class
type construct, so they were not encapsulated objects - does
anyone remember any such beasts? They were before I joined the
OOP circus in the mid '80s.)

The ability to hide the internal data was called data hiding
(originally coined by David Parnas talking about modular
programming I believe). Encapsulation and Data Hiding were thus
two different things and Encapsulation (like Python does) was an
essential feature of OOP along with Polymorphism (whether based
on inheritance (like C++) or inteface (like Python)). Data Hiding
was desirable but optional (several OOP languages provide
encapsulation and not Data Hiding). Similarly some OOP languages
provide polymorphism by other means than inheritance and so
inheritance also was desirable but not essential for OOP (but
that has always been a point of some debate).

I've been trying to find the exact point when encapsulation
changed its meaning to data hiding but without success. It seems
to have been around about 1990-2 somewhere...
</RANT>

Sorry, just had to get that off my chest!

Alan G.


Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 
Reply With Quote
 
Jacek Generowicz
Guest
Posts: n/a
 
      09-03-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alan Gauld) writes:

> On Tue, 02 Sep 2003 16:34:37 +0200, Bertel Lund Hansen
> <(E-Mail Removed)> wrote:
> > "Encapsulation" is the word that describes the effort(s) to make
> > it impossible for the user of a module to access the attributes
> > (and hidden methods) directly and only allow him to do it through
> > public methods with build-in check so that no disasters occur.

>
> <RANT>
> Does anyone else really hate this corruption as much as me?


Yes. Me.

> Encapsulation in OO originally meant the ability to bind data and
> functions together into a single thing (that we call an object).


> The ability to hide the internal data was called data hiding


I make a point of stating this in my Python courses.

> Polymorphism (whether based on inheritance (like C++) or inteface
> (like Python)).


Yes, I also make a point stating that (dynamic) polymorphism is
orthogonal to inheritance (unless your language is fundamentally
flawed

> I've been trying to find the exact point when encapsulation
> changed its meaning to data hiding but without success.


I don't think that it has changed its meaning; most people just use
the term incorrectly.

Similarly, I don't think that the meaning of "pizza's" has changed
just because most people insist on writing "pizza's" when they really
mean "pizzas" (as in "please give me five pizza's")[1].

I have similar feelings about the orthogonality of the static-dynamic
and strong-weak typing axes ... just because most people say that
Python is weakly typed, doesn't mean that it is, or that the meaning
of "weakly typed" has changed.

But I guess that I am plain wrong: given that there is no Academy of
English to govern its use, majority use determines meaning.

> Sorry, just had to get that off my chest!


I know the feeling. It was only a lack of time that prevented me from
doing so myself, on first reading of the original.



[1] Of cour'se, this is not about pizza's, but the abu'se of
apostrophe's in general.
 
Reply With Quote
 
Cousin Stanley
Guest
Posts: n/a
 
      09-03-2003
| ...
} I don't think that it has changed its meaning;
| most people just use the term incorrectly.
| ...

Add to these the OFTEN confused terms
accuracy and precision ...

--
Cousin Stanley
Human Being
Phoenix, Arizona


 
Reply With Quote
 
Bob Gailer
Guest
Posts: n/a
 
      09-03-2003
At 10:04 AM 9/3/2003 -0700, Cousin Stanley wrote:

>| ...
>} I don't think that it has changed its meaning;
>| most people just use the term incorrectly.
>| ...
>
> Add to these the OFTEN confused terms
> accuracy and precision ...


Precisely what I was thinking.

I looked at a digital thermometer in Radio Shack. It had been in the store
long enough to stabilize. It showed indoor and outdoor temperatures to a
precision of 0.1 degree. The indoor and outdoor readings differed by about
a degree. So now we know the accuracy, and the precision was therefore
worthless.

Bob Gailer
(E-Mail Removed)
303 442 2625


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003

 
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 Graham Fawcett Python 1 09-02-2003 01:05 PM
Re: Polymorphism the Python way Peter Otten Python 1 09-02-2003 01:03 PM



Advertisments