Overloaded Constructors?!?
Michael Spencer
mahs at telcopartners.com
Sun Mar 20 18:27:04 EST 2005
andrea_gavana at tin.it wrote:
> [trying to create a single Python class with the equivalent of the following overloaded constructors]
>
> wxFoldWindowItem(wxWindow *wnd, int flags = wxFPB_ALIGN_WIDTH,
> int ySpacing = wxFPB_DEFAULT_YSPACING,
> int leftSpacing = wxFPB_DEFAULT_LEFTSPACING,
> int rightSpacing = wxFPB_DEFAULT_RIGHTSPACING)
>
> or
>
> wxFoldWindowItem(int y, const wxColour &lineColor = *wxBLACK,
> int ySpacing = wxFPB_DEFAULT_YSPACING,
> int leftSpacing = wxFPB_DEFAULT_LEFTLINESPACING,
> int rightSpacing = wxFPB_DEFAULT_RIGHTLINESPACING)
Several options in addition to the factory function that Kent has suggested (and
I'll assume he'll follow up with clarification)
# Option 1: one abstract base class holding all/most of the methods, and two
separate subclasses reflecting the two use-cases.
class _AbstractFoldWindowItem(object):
def __init__(self, *args, **kw):
raise NotImplemetedError
def
... all the shared methods
then
class FoldWindowItem(_AbstractFoldWindowItem):
def __init__(self, wxWindow, flags = wxFPB_ALIGN_WIDTH,
ySpacing = wxFPB_DEFAULT_YSPACING,
leftSpacing = wxFPB_DEFAULT_LEFTSPACING,
rightSpacing = wxFPB_DEFAULT_RIGHTSPACING):
class FoldSeparator(_AbstractFoldWindowItem):
def __init__(self, y, lineColor = wx.BLACK,
ySpacing = wx.FPB_DEFAULT_YSPACING,
leftSpacing = wx.FPB_DEFAULT_LEFTLINESPACING,
rightSpacing = wx.FPB_DEFAULT_RIGHTLINESPACING):
# Option 2: One class, two constructors:
class FoldWindowItem():
def __init__(self, wxWindow, flags = wxFPB_ALIGN_WIDTH,
ySpacing = wxFPB_DEFAULT_YSPACING,
leftSpacing = wxFPB_DEFAULT_LEFTSPACING,
rightSpacing = wxFPB_DEFAULT_RIGHTSPACING):
"""Initializes with wxWindow"""
self._type = wx.Window
@classmethod
def FromSeparator(cls, y, lineColor = wx.BLACK,
ySpacing = wx.FPB_DEFAULT_YSPACING,
leftSpacing = wx.FPB_DEFAULT_LEFTLINESPACING,
rightSpacing = wx.FPB_DEFAULT_RIGHTLINESPACING):
newobj = cls.__new__(y, lineColor = wx.BLACK,
ySpacing = wx.FPB_DEFAULT_YSPACING,
leftSpacing = wx.FPB_DEFAULT_LEFTLINESPACING,
rightSpacing = wx.FPB_DEFAULT_RIGHTLINESPACING)
newobj._type = wx.SEPARATOR
# other initializatio
return newobj
This requires the user code to call the class in two ways depending on how it is
to be used:
i.e., myFoldWindowItem = FoldWindowItem(Window)
or myFoldWindowItem = FoldWindowItem.FromSeparator(y)
# Option 3: inspect the arguments and provide the signature details in the docstring
class FoldWindowItem():
def __init__(self, obj, **kw):
"""Initialize with:
wxWindow, flags = wxFPB_ALIGN_WIDTH,
ySpacing = wxFPB_DEFAULT_YSPACING,
leftSpacing = wxFPB_DEFAULT_LEFTSPACING,
rightSpacing = wxFPB_DEFAULT_RIGHTSPACING)
or:
y, lineColor = wx.BLACK,
ySpacing = wx.FPB_DEFAULT_YSPACING,
leftSpacing = wx.FPB_DEFAULT_LEFTLINESPACING,
rightSpacing = wx.FPB_DEFAULT_RIGHTLINESPACING)"""
if isinstance(obj, wx.Window):
# Do one thing
elif isinstance(obj, wx.SEPARATOR):
# Do the other
HTH
Michael
More information about the Python-list
mailing list