Extending classes __init__behavior for newbies

rantingrick rantingrick at gmail.com
Sun Feb 13 18:24:34 EST 2011


On Feb 13, 5:11 pm, James Mills <prolo... at shortcircuit.net.au> wrote:
> On Mon, Feb 14, 2011 at 8:39 AM, rantingrick <rantingr... at 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.






More information about the Python-list mailing list