Using globals with classes

Scott David Daniels Scott.Daniels at Acm.Org
Fri Aug 12 13:36:25 EDT 2005


Madhusudan Singh wrote:
> .... I am using qwtplot to display a running plot :
> 
> void Form3::runningplot(n,plottitle,xname,x,y1name,y1,y2name,y2)
> {
^^ I presume this is just some untranslated stuff ^^
> if n==1 :
> 
> plotkey1=self.runningqwtPlot.insertCurve(y1name,self.runningqwtPlot.xBottom,self.runningqwtPlot.yLeft)
> 
> plotkey2=self.runningqwtPlot.insertCurve(y2name,self.runningqwtPlot.xBottom,self.runningqwtPlot.yRight)
>         self.runningqwtPlot.setTitle(plottitle)
>         self.runningqwtPlot.setXGrid(True)
>         self.runningqwtPlot.setAxisAutoScale(self.runningqwtPlot.yLeft)
>         self.runningqwtPlot.setAxisAutoScale(self.runningqwtPlot.yRight)
>         self.runningqwtPlot.setAxisAutoScale(self.runningqwtPlot.xBottom)
>         self.runningqwtPlot.setAxisTitle(self.runningqwtPlot.yLeft,y1name)
>         self.runningqwtPlot.setAxisTitle(self.runningqwtPlot.yRight,y2name)
>         self.runningqwtPlot.setAxisTitle(self.runningqwtPlot.xBottom,xname)
>         self.runningqwtPlot.setCurveData(plotkey1,x,y1,n)
>         self.runningqwtPlot.setCurveData(plotkey2,x,y2,n)
>         self.runningqwtPlot.replot()
> else :
>  self.runningqwtPlot.setCurveData(plotkey1,x,y1,n)
>  self.runningqwtPlot.setCurveData(plotkey2,x,y2,n)
>  self.runningqwtPlot.replot()
> }

The way I'd normally accomplish this is to separate the setup and use
by defining a class:

class CurvePlot(object):
     def __init__(self, plot, plottitle, xname, y1name, y2name,
                  key1=None, key2=None):
         self.plot = plot
         if key1 is None:
             key1 = plot.insertCurve(y1name, plot.xBottom, plot.yLeft)
         self.key1 = key1
         if key2 is None:
             key2 = plot.insertCurve(y2name, plot.xBottom, plot.yRight)
         self.key2 = key2
         plot.setTitle(plottitle)
         plot.setXGrid(True)
         plot.setAxisAutoScale(plot.yLeft)
         plot.setAxisAutoScale(plot.yRight)
         plot.setAxisAutoScale(plot.xBottom)
         plot.setAxisTitle(plot.yLeft, y1name)
         plot.setAxisTitle(plot.yRight, y2name)
         plot.setAxisTitle(plot.xBottom, xname)

     def curve(self, x, y1, n)
             self.plot.setCurveData(self.key1, x, y1, n)
             self.plot.setCurveData(self.key2, x, y2, n)
             self.plot.replot()

And then calling it like:

     cplot = CurvePlot(self.runningqwtPlot, plottitle,
                       xname, y1name, y2name)
     cplot.curve(n, x, y1, y2)

> I also have a global variable named "globaldebug" that when set to True,
> shows some diagnostic information as different slots are called. That too
> fails with the same error :
> 
> NameError: global name 'globaldebug' is not defined
What you probably don't understand is that "globals" are per-module, not
program-wide.  If you write a global from inside a function or method,
you need to declare "global varname" inside the function or method in
which you do the writing.  Simply using (reading) a global in a module
does not require the "global" declaration.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list