New subclass vs option in __init__

K

Kurt Smith

Hi List:

Class inheritance noob here.

For context, I have the following base class and subclass:

class Base(object):
def __init__(self, val):
self.val = val

class Derived1(Base):
def __init__(self, val):
super(Derived1, self).__init__(val)

I'm curious as to other's thoughts on the following: when
incorporating optional behavior differences for a subclass, do you a)
make a new subclass (e.g., 'Derived2') and override (and add new)
methods that would encapsulate the new behavior, or b) keep the same
subclass around (i.e., 'Derived1'), but add an initialization option
that would specify the different behavior, and check for the value of
this option in the different methods?

It would seem that there are cases where one would be preferable over
the other: a) when the new behavior would modify a large portion of
the existing subclass, making a new subclass would be ideal; b) when
the new behavior changes only slightly the existing subclass, perhaps
a simple default option in the subclass's __init__ method would be
best. Where is the tipping point? Since one cannot predict what
direction the new behavior might take things, should one usually err
on the side of a new subclass? Is option b) just being lazy? Is a)
too verbose in many situations?

Kurt
 
B

Bruno Desthuilliers

Kurt Smith a écrit :
Hi List:

Class inheritance noob here.

For context, I have the following base class and subclass:

class Base(object):
def __init__(self, val):
self.val = val

class Derived1(Base):
def __init__(self, val):
super(Derived1, self).__init__(val)

I'm curious as to other's thoughts on the following: when
incorporating optional behavior differences for a subclass, do you a)
make a new subclass (e.g., 'Derived2') and override (and add new)
methods that would encapsulate the new behavior, or b) keep the same
subclass around (i.e., 'Derived1'), but add an initialization option
that would specify the different behavior, and check for the value of
this option in the different methods?

You forgot the strategy pattern : extract the different behaviours into
other objects (in Python, usually callback functions or custom
callables) that are passed to the initializer and called when
appropriate. This keeps a clean encapsulation of variations (ie you
don't have to add new conditions and tests in your class) while avoiding
class proliferation.

Inheritance - while useful - is a bit oversold IMHO, specially in the
context of a dynamic language. FWIW, inheritance is just a special case
of composition/delegation...
 
C

Carl Banks

It would seem that there are cases where one would be preferable over
the other: a) when the new behavior would modify a large portion of
the existing subclass, making a new subclass would be ideal; b) when
the new behavior changes only slightly the existing subclass, perhaps
a simple default option in the subclass's __init__ method would be
best. Where is the tipping point?


Good question.


Carl Banks
 
N

Neil Cerutti

Good question.

The major factor in the tipping point is clarity. And simplicity.
The two major factors in deciding the tipping point are: clarity,
simplicity, and extensibility. ... The THREE major tipping point
factors ARE: clarity, simplicity, extensibility. And efficiency.
Among the many factors in deciding the tipping point are: (etc.,
etc.)
 
C

Carl Banks

The major factor in the tipping point is clarity. And simplicity.
The two major factors in deciding the tipping point are: clarity,
simplicity, and extensibility. ... The THREE major tipping point
factors ARE: clarity, simplicity, extensibility. And efficiency.
Among the many factors in deciding the tipping point are: (etc.,
etc.)

...., lots of experience, 20/20 foresight, a good Ouija board,
luck, ....


Carl Banks
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top