Re: Extending classes __init__behavior for newbies

Discussion in 'Python' started by James Mills, Feb 13, 2011.

  1. James Mills

    James Mills Guest

    On Mon, Feb 14, 2011 at 7:17 AM, Benjamin J. Racine
    <> wrote:
    > I don't quite understand the interplay of the two different __init__ methods
    > when trying to extend a class.  Below is my hack attempt at doing so....
    > class ship(object):
    >     def __init__(self,l=0,b=0,t=0,name=''):
    >       self.l = l
    >         self.b = b
    >         self.t = t
    >         self.name = name
    >     def make_it_deeper(self,t):
    >         self.t = self.t - t
    > class fast_ship(ship):
    >     def __init__(self,speed = 0):
    >         self.speed = speed
    > my_ship = fast_ship(l = 100,b = 50, t = 10, name = 'cutter',speed = 10)
    >
    > If anyone would be so kind as to show me how to get the intended effect that
    > I've hinted at, I would be most grateful,


    When you subclass a base class (ship in your example) you need
    to call it's parent (or super) methods. This includes the constructor
    (__init__).

    The standard way of doing this in Python is:

    class FasterShip(Ship):

    def __init__(self, l=0,b=0,t=0,name='', speed=0):
    super(FasterShip, self).__init__(l, b, t, name)

    self.speed = speed

    This ensures that the constructor of the base class (Ship) gets called
    and the object initialized with the attributes you've defined.

    Note for convention reasons I've also included proper class names
    in the example. Classes normally start with an Upper case letter.

    cheers
    James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
     
    James Mills, Feb 13, 2011
    #1
    1. Advertising

  2. James Mills

    rantingrick Guest

    On Feb 13, 4:00 pm, James Mills <> wrote:

    [snip]

    > When you subclass a base class (ship in your example) you need
    > to call it's parent (or super) methods. This includes the constructor
    > (__init__).
    >
    > The standard way of doing this in Python is:
    >
    > class FasterShip(Ship):
    >
    >     def __init__(self, l=0,b=0,t=0,name='', speed=0):
    >         super(FasterShip, self).__init__(l, b, t, name)



    Is super really necessary here James? I think the use of super in this
    manner is just as confusing to noobs as using map/filter are to list
    comprehensions/generators. However in exactly the opposite
    "syntactical" way since dumping super reduces syntax. Not trying to
    pick on you, just curious.
     
    rantingrick, Feb 13, 2011
    #2
    1. Advertising

  3. James Mills

    rantingrick Guest

    On Feb 13, 4:00 pm, James Mills <> wrote:

    > class FasterShip(Ship):
    >
    >     def __init__(self, l=0,b=0,t=0,name='', speed=0):
    >         super(FasterShip, self).__init__(l, b, t, name)
    >
    >         self.speed = speed


    Did everyone miss the fact that this inheritance is unnecessary?
    Considering the Ship class has an attribute "speed" that will be
    affected (either directly or indirectly) by simply modifying it?

    faster_ship = Ship()
    faster_ship.speed = someFasterSpeed
     
    rantingrick, Feb 13, 2011
    #3
  4. James Mills

    James Mills Guest

    On Mon, Feb 14, 2011 at 8:39 AM, rantingrick <> wrote:
    > Did everyone miss the fact that this inheritance is unnecessary?
    > Considering the Ship class has an attribute "speed" that will be
    > affected (either directly or indirectly) by simply modifying it?


    The attribute "speed" was not a member of the
    OP's class ship.

    cheers
    James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
     
    James Mills, Feb 13, 2011
    #4
  5. James Mills

    rantingrick Guest

    On Feb 13, 5:11 pm, James Mills <> wrote:
    > On Mon, Feb 14, 2011 at 8:39 AM, rantingrick <> wrote:
    > > Did everyone miss the fact that this inheritance is unnecessary?
    > > Considering the Ship class has an attribute "speed" that will be
    > > affected (either directly or indirectly) by simply modifying it?

    >
    > The attribute "speed" was not a member of the
    > OP's class ship.


    My solution still stands...

    >>> class Ship(object):

    pass

    >>> faster_ship = Ship()
    >>> faster_ship.speed = 10


    ....if the creation of instance variable "speed" is all that is needed
    then the inheritance is still unnecessary. Since "speed" did not exist
    in the base, then one can reason that no method of Ship could have
    modified "speed" as that modification would raise an attribute error
    UNLESS Ship is an ABC!

    OR

    Unless the OP creates *some* instance method of FasterShip that
    operates on the instance variable "speed" it would be redundant to use
    inheritance. However if an instance method of FasterShip required the
    instance variable "speed" then we have reason for inheritance.


    So true if Ship is an ABC or if any FasterShip method requires a
    "speed" instance variable.
     
    rantingrick, Feb 13, 2011
    #5
    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. PATRICE

    newbies and quartus

    PATRICE, Jun 14, 2004, in forum: VHDL
    Replies:
    2
    Views:
    585
    Ken Smith
    Jun 15, 2004
  2. Rob Richardson

    Other newsgroups for newbies?

    Rob Richardson, Mar 16, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    340
    Juan T. Llibre
    Mar 16, 2006
  3. Joona I Palaste

    Question about newbies and interfaces

    Joona I Palaste, Nov 30, 2003, in forum: Java
    Replies:
    2
    Views:
    337
    Chris Uppal
    Nov 30, 2003
  4. Alf P. Steinbach

    How we recognize newbies as newbies

    Alf P. Steinbach, Aug 13, 2007, in forum: C++
    Replies:
    31
    Views:
    1,118
    Diego Martins
    Aug 21, 2007
  5. James Mills
    Replies:
    21
    Views:
    602
    Westley Martínez
    Feb 16, 2011
Loading...

Share This Page