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 4 3442
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...
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Frank Millman |
last post by:
Hi all
I have a question regarding inheritance. I have come up with a
solution, but it is not very elegant - I am sure there is a more
pythonic approach. Assume the following class definitions....
|
by: Kenneth McDonald |
last post by:
I'm attempting to create a subclass of 'str' that I can abitrarily
initialize at creation time. As an illustration, this gives the
flavor of what I'm trying to do:
class AlwaysLower(str):
def...
|
by: Gerry Sutton |
last post by:
Hi All!
I have noticed a strange behavior when using a constant identifier to
initialize an instance list variable in a base class and then trying to
modifying the list in subclasses by using...
|
by: flupke |
last post by:
I have the following test code setup, trying to get the class name of a
subclass in the super class. (Reason why i want this is described below)
file class_name_start.py
========================...
|
by: s.lipnevich |
last post by:
Hi All,
Is anything wrong with the following code?
class Superclass(object):
def __new__(cls):
# Questioning the statement below
return super(Superclass, cls).__new__(Subclass)
class...
|
by: damacy |
last post by:
hi, there. i have a problem writing a program which can obtain ip
addresses of machines running in the same local network.
say, there are 4 machines present in the network; , , and
and if i...
|
by: davidfinance |
last post by:
Ok, maybe this is a stupid question, but why can't I make a subclass of
datetime.date and override the __init__ method?
---
from datetime import date
class A(date):
def __init__(self, a,...
|
by: mkppk |
last post by:
I have kind of strange change I'd like to make to the sets.Set()
intersection() method..
Normally, intersection would return items in both s1 and s2 like with
something like this: ...
|
by: Yves Dorfsman |
last post by:
I want to create a subclass of 'file' but need to open the file with os.open
(because I want to open it in exclusive mode), and need an additional method.
Because I need an additional method, I...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |