I'm not sure you can control that, as the decision as to whether args or
kwargs is used is made by the caller of the function.
i.e. the caller decides whether to code as :
MyParrot( 34.3, Pink, 'Alive' )
or
MyParrot( DeadOrAlive = 'Alive', Price = 34.3, Coulour = Pink )
Both calls are completely valid.
I've also found that if you use subclass a few times, then it is
possible to have same keyword appearing multiple times (explicitly and
in kwargs) and a runtime error is generated. I have thus resorted to
specifying all arguments as kwargs, and popping values that are not for
the base class, and specifying kwargs defaults where desired.
class MyPanel( wx.Panel ) :
def __init__( self, *args, **kwargs ) :
fgcolour = kwargs.pop( 'fgcolour' )
bgimage = kwargs.pop( 'bgimage', None )
kwargs['title'] = kwargs.get( 'title', 'MyPanel' )
wx.Panel.__init__( self, *args, **kwargs )
instead of :
class MyPanel( wx.Panel ) :
def __init__( self, parent, fgcolour, title = 'MyPanel', bgimage
= None ) :
wx.Panel.__init__( self, parent = parent, title = title )
or :
class MyPanel( wx.Panel ) :
def __init__( self, fgcolour, bgimage = None, *args, **kwargs ) :
kwargs['title'] = kwargs.get( 'title', 'MyPanel' )
wx.Panel.__init__( self, *args, **kwargs )
The second method is more standard python and easier to read and understand.
The kwargs method is not as easy to understand at a glance, and it gets
uglier the more parameters there are. I'm also not sure how efficient
it is in terms of performance (presumably ok). However it does have the
advantage that the order of arguments is not important (as is the case
when using constructor defaults), and the problem of multiple keywords
does not occur (important). Maybe just deleting potential multiple
keywords from kwargs is more elegant ??
This still does not cater for args, but if I want to force the caller to
use keywords (which I probably do as it makes code more readable and
reliable), then I guess I could just leave out the args altogether.
An interesting quandary for the style guide. Hopefully a good solution
can be agreed upon and the style guide updated
Cheers, Brendan.
···
On 16/05/10 4:43 PM, GadgetSteve wrote:
The "Style Guide for wxPython code" recommends to using *args and
**kwargs when subclassing wx.Windows.
2b. Use *args and **kwargs when subclassing wx.Windows
class MyPanel( wx.Panel ) :
def __init__( self, *args, **kwargs ) :
wx.Panel.__init__( self, *args, **kwargs )
I would like to know what is recommended when using extra parameters for
the subclass.
Should the parameters be explicitly specified, or through **kwargs ??
class MyPanel( wx.Panel ) :
def __init__( self, bgimage = None, *args, **kwargs ) :
wx.Panel.__init__( self, *args, **kwargs )
or
class MyPanel( wx.Panel ) :
def __init__( self, *args, **kwargs ) :
wx.Panel.__init__( self, *args, **kwargs )
bgimage = kwargs.get( 'bgimage', None )
or some other variation ??
e.g. I'm not sure if the *args should be handled first or can be safely
ignored. I assume, theoretically, it should be handled first, then
search kwargs ... correct ??
If so, then it seems to make the constructor methods overly complicated
for each parameter.
AFAIK the normal process is to use as args all the parameters that the
user *must* supply and as kwargs those that are optional, i.e. if the
documentation of your constructor looks something like:
MyClass MyParrot(Price, Colour=Blue, DeadOrAlive='Dead')
Then Price would the args and Colour & DeadOrAlive would be kwargs.
--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en