By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

New subclass vs option in __init__

P: n/a
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
Dec 6 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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...
Dec 6 '07 #2

P: n/a
On Dec 6, 11:56 am, "Kurt Smith" <kwmsm...@gmail.comwrote:
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

Dec 7 '07 #3

P: n/a
On 2007-12-07, Carl Banks <pa************@gmail.comwrote:
On Dec 6, 11:56 am, "Kurt Smith" <kwmsm...@gmail.comwrote:
>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.
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.)

--
Neil Cerutti
Dec 7 '07 #4

P: n/a
On Dec 7, 9:36 am, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-12-07, Carl Banks <pavlovevide...@gmail.comwrote:
On Dec 6, 11:56 am, "Kurt Smith" <kwmsm...@gmail.comwrote:
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.

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
Dec 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.