Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Extending classes __init__behavior for newbies (http://www.velocityreviews.com/forums/t743499-re-extending-classes-__init__behavior-for-newbies.html)

James Mills 02-13-2011 10:00 PM

Re: Extending classes __init__behavior for newbies
 
On Mon, Feb 14, 2011 at 7:17 AM, Benjamin J. Racine
<bjracine@glosten.com> 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"

rantingrick 02-13-2011 10:25 PM

Re: Extending classes __init__behavior for newbies
 
On Feb 13, 4:00*pm, James Mills <prolo...@shortcircuit.net.au> 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 02-13-2011 10:39 PM

Re: Extending classes __init__behavior for newbies
 
On Feb 13, 4:00*pm, James Mills <prolo...@shortcircuit.net.au> 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


James Mills 02-13-2011 11:11 PM

Re: Extending classes __init__behavior for newbies
 
On Mon, Feb 14, 2011 at 8:39 AM, rantingrick <rantingrick@gmail.com> 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"

rantingrick 02-13-2011 11:24 PM

Re: Extending classes __init__behavior for newbies
 
On Feb 13, 5:11*pm, James Mills <prolo...@shortcircuit.net.au> wrote:
> On Mon, Feb 14, 2011 at 8:39 AM, rantingrick <rantingr...@gmail.com> 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.





All times are GMT. The time now is 06:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.