[Tutor] passing arguments to a make a graph

Alan Gauld alan.gauld at btinternet.com
Sat Mar 24 01:32:53 CET 2007

"Che M" <pine508 at hotmail.com> wrote

>>Instead of using the class create an instance(aka object)
>>        self.plotPanel = PlotPanel(mypoints,self.notebook1)

> This worked great, thanks!  One question:  you mention that in this 
> way it
> is passing in the mypoints list and also the notebook1.  What I am 
> unclear
> is, why are you allowed to pass in the notebook?  If you look at the
> original class PlotPanel():
> class PlotPanel(wx.lib.plot.PlotCanvas):
>    def __init__(self, points=[], *args, **kwargs):
>        wx.lib.plot.PlotCanvas.__init__(self, *args, **kwargs)

OK, It always amazes me how the same basic topic comes
up all at once in this group. Kent gave a good explanation
of *args/**kwargs earlier this week, look back for it.

But basically what those two magic words do is take all of
the arguments passed in and bundle them up into those two
collections(kw args is keyword arguments and args is the rest(*))
So then when you call PlotCanvas.__init__() you are just passing
on everything that was passed into your init. You don;t know
nor care how many other arguments were gibemn, you just
pass them onto the superclass to deal with.

But you do care about points, so you put that as the first parameter.
Then when your init gets called Python peels off the first value
and puts it in points, the rest get put into args/kwargs.

(*)You recall that if a function has all of its parameters take
default values you can call it using keywords like this:

def f(a=1,b=2,c=3,d=4,e=3,f=2,g=1):



Those are the keyword arguments
But I can also call it with plain values provided I use the right 

f(9,8,7)  # a=9,b=8,c=7,d=4,e=3,f=2,g=1

Those are the args
Now I can use both techniques too:

f(7,8,e=0,g=2)  # a=7,b=8,c=3,d=4,e=0,f=2,g=2

Here args is 7,8
kwargs is e=0,g=2

Is that clearer?

> **kwargs.  But you are passing it the notebook1, so how does it know 
> which
> "slot" of these four parameters notebook1 refers to?

It doesn't, it passes notebook1 as the first value in args to the 
superclass init.
It then gets unpacked as the first parameter(after self) of the 
superclass' init.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list