Label-Value (was: Re: Inheriting the @ sign from Ruby)

Fredrik Lundh fredrik at effbot.org
Tue Dec 12 20:49:21 CET 2000


Daniel Wood wrote:
> Here is what I wanted to do.  I have some random parameters, lambda
> and beta.  My calculation uses them like so:
>
> plot( lambda, beta )

lambda is a reserved word, so I doubt that's what you're
doing.  Let's call it "spam" instead.

> I'm using pygtk as a widget set.  I have some sliders.  I wanted to
> make one of the sliders control lambda and one control beta.  The
> control should be scaled by some parameter etc, etc.  Now I could have
> a function
>
> def lambdaCB( val ):
>  global lambda
>  lambda = val / constant
>
> but instead I thought that I would have a simple class
>
> class ScaleCB:
>   def __init__( self, variable, scale ):
>     self.variable = variable
>     self.scale = scale
>
>   def __call__( self, val )
>     self.variable = val / self.scale
>
> (then, I can do lots of parameters with nice simple code instead of
> bunches of stupid helper functions.
>
> This obviously didn't work.

(didn't do what you wanted it to do, at least)

How about this one:

    class PlotParams:
        def __init__(self):
            self.spam = 1
            self.beta = 2
        def setspam(self, value):
            self.spam = value
            self.redraw()
        def setbeta(self, value):
            self.beta = value
            self.redraw()
        def redraw(self):
            plot(self.spam, self.beta)

    myplot = PlotParams()

    spamScaleWidget.setcallback(myplot.setspam)
    betaScaleWidget.setcallback(myplot.setbeta)

> I *think* that the "pythonic" sol'n appears to be lambda = [ value ]
> or some such.

Besides being a syntax error, it doesn't look very Pythonic
to me.  If you want to change a named value, the best way
is to make it an attribute of a class.

</F>





More information about the Python-list mailing list