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 3458
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |