Re: Polymorphism the Python way

Discussion in 'Python' started by Graham Fawcett, Aug 31, 2003.

  1. 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
     
    Graham Fawcett, Aug 31, 2003
    #1
    1. Advertising

  2. Graham Fawcett

    Daniel Klein Guest

    On Sun, 31 Aug 2003 03:47:49 -0400, Graham Fawcett
    <> 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
     
    Daniel Klein, Sep 2, 2003
    #2
    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. Michael Peuser

    Re: Polymorphism the Python way

    Michael Peuser, Aug 31, 2003, in forum: Python
    Replies:
    6
    Views:
    1,790
    Bob Gailer
    Sep 3, 2003
  2. Peter Otten

    Re: Polymorphism the Python way

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

    polymorphism in python

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

Share This Page