[Tutor] passing arguments via an instance of a class

Che M pine508 at hotmail.com
Wed Apr 4 18:09:56 CEST 2007


John Fouhy said:
>Can you post the full error message (including stack trace and line
>numbers) and the code for the last function in the stack trace?
>(i.e. the code where the NameError occurs)

It is now working (so I won't post the code/errors unless you think it is 
useful for people to see), and the corrected code is shown below.

Alan Gauld said:

>you could do:
>
>x = self.xpoints
>y = self.ypoints
>
>Or just wait and use self.xpoints etc at the point of use.

Thank you, Alan.  My whole problem is not fully understanding the self (to 
paraphrase Socrates...) and making sure to specify it.  I did change this in 
this way.  Also...

>#This is now the button which is supposed to feed the PlotPanel
>#the points it needs.  It is a method of the wxFrame (code not shown)
> >
> >def OnButton1Button(self, event):
> >#these are the points I want to be plotted when I push this button
> >        xpoints=[2,4,6]
> >        ypoints=[10,20,30]
> >        self.weightplot = PlotPanel(self.notebook1,xpoints,ypoints)
>
>But this needs to use self to access the member values:
>
>         self.weightplot = 
>PlotPanel(self.notebook1,self.xpoints,self.ypoints)

Doing it this way didn't work--it gave the error:

AttributeError: 'Frame1' object has no attribute 'xpoints'

because, I think, self in this case refers to the whole frame, and not 
PlotPanel (i.e. notebook1 is an attribute of the Frame but xpoints and 
ypoints are meant to be attributes of PlotPanel).  The button was an object 
under the whole wxFrame (not sure I mentioned that), so self referred to the 
frame, not the PlotPanel, I think.  But writing it this way:

          self.weightplot = PlotPanel(self.notebook1,xpoints,ypoints)

did work.  Which is the same situation as what you helped me with last week. 
  The difference in this case is that I was, wrongly, not telling draw() to 
plot self.xpoints (I was giving just xpoints) and the draw() method could 
not know which instance this is from.  Is that right?

To summarize (just to have this clear for myself and other beginners), these 
are the lines which mattered:

1. The init had to have the xpoints and ypoints lists included so it could 
"accept" the lists when needed:

    def __init__(self, parent, xpoints=[], ypoints=[], id = -1, color = 
None,\
        dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs):

2. The x and y lists had to refer to self.xpoints and self.ypoints (or I 
could have done it the other
way Alan mentioned):

    x = self.xpoints
    y = self.ypoints

3. I had to have an instance of the PlotPanel which was passed the parent 
notebook and the lists:

    self.weightplot = PlotPanel(self.notebook1,xpoints,ypoints)

4. And then I just added this self.weightplot PlotPanel instance as a page 
to my notebook:

    self.notebook1.AddPage(imageId=-1, page=self.weightplot,
                select=True, text='Test Plot)

Thanks Alan for your help! and John for your offer to debug!--it's great to 
understand this better and to have this simple plot actually display, very 
encouraging.  I guess I really have to work on my "self" control.  :)

-Che

_________________________________________________________________
MSN is giving away a trip to Vegas to see Elton John.  Enter to win today. 
http://msnconcertcontest.com?icid-nceltontagline



More information about the Tutor mailing list