Re: Polymorphism the Python way

Discussion in 'Python' started by Michael Peuser, Aug 31, 2003.

  1. "Daniel Klein" <> schrieb im Newsbeitrag
    news:...
    > 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
    Michael Peuser, Aug 31, 2003
    #1
    1. Advertising

  2. Michael Peuser

    Daniel Klein Guest

    On Sun, 31 Aug 2003 09:37:00 +0200, "Michael Peuser" <>
    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
    Daniel Klein, Sep 2, 2003
    #2
    1. Advertising

  3. 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
    Bertel Lund Hansen, Sep 2, 2003
    #3
  4. Michael Peuser

    Alan Gauld Guest

    On Tue, 02 Sep 2003 16:34:37 +0200, Bertel Lund Hansen
    <> 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
    Alan Gauld, Sep 2, 2003
    #4
  5. (Alan Gauld) writes:

    > On Tue, 02 Sep 2003 16:34:37 +0200, Bertel Lund Hansen
    > <> 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.
    Jacek Generowicz, Sep 3, 2003
    #5
  6. | ...
    } 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
    Cousin Stanley, Sep 3, 2003
    #6
  7. Michael Peuser

    Bob Gailer Guest

    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

    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
    Bob Gailer, Sep 3, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Graham Fawcett

    Re: Polymorphism the Python way

    Graham Fawcett, Aug 31, 2003, in forum: Python
    Replies:
    1
    Views:
    318
    Daniel Klein
    Sep 2, 2003
  2. Peter Otten

    Re: Polymorphism the Python way

    Peter Otten, Aug 31, 2003, in forum: Python
    Replies:
    1
    Views:
    304
    Daniel Klein
    Sep 2, 2003
  3. rashkatsa

    polymorphism in python

    rashkatsa, Nov 26, 2003, in forum: Python
    Replies:
    7
    Views:
    8,074
    Jay O'Connor
    Nov 26, 2003
  4. Krivenok Dmitry
    Replies:
    13
    Views:
    1,422
    Axter
    Jun 1, 2006
  5. Stephan Mueller
    Replies:
    7
    Views:
    620
    Stephan Mueller
    Aug 7, 2005
Loading...

Share This Page