[wxPython-users] parameter passing Question

Christopher Barker Chris.Barker at noaa.gov
Fri Aug 4 13:59:36 PDT 2006


Daniel Johnson wrote:
> I tried your way....but again I get error.

What's the error?

>  From OnEdit function of MyFram1 I wrote
> 
> win = MyFrame2(self, sim_name, *args, **kwds)

This should be:
win = MyFrame2(sim_name, self, *args, **kwds)

And only that if you want to pass in the args and kwargs that are 
defined, and they may not be defined at all in that namespace.

In this case "self" is the parent of MyFrame (or you could pass None). 
You want that passed into the wx.Frame.__init__

> and changed MyFrame2 to the following form
> class MyFrame2(wx.Frame):
>    def __init__(self, sim_name_accept, *args, **kwds):

In this case "self" is the instance. It alwys gets passed in as the 
first argument of any method.

*args means: put all the unamed arguments into a tuple called args.
**kwargs means: put all keyword arguments into a dict called kwargs

It's the "*" that is important, the names are jsut conventions.

That construction is used if you want to simppy pass all the arguemnts 
to a class __init__ on to the superclass constructor.

you can get a sense of how all that *args stuff works, by playing with it:
 >>> def f(p, *args, **kwargs):
...   print "p", p
...   print "*args", args
...   print "**kwargs", kwargs
...
 >>>
 >>> f(4)
p 4
*args ()
**kwargs {}
 >>> f(4,5,6)
p 4
*args (5, 6)
**kwargs {}
 >>> f(4,6,7,this=8)
p 4
*args (6, 7)
**kwargs {'this': 8}
 >>>


-Chris




-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list