"configuring" a class

R

Russ

I would like to let the user of one of my classes "configure" it by
activating or de-activating a particular behavior (for all instances of
the class).

One way to do this, I figured, is to have a static class variable,
along with a method to set the variable. However, I am stumped as to
how to do that in python. Suggestions welcome.

The next best thing, I figure, is to just use a global variable.
Several "methods" of the class then check the value of the global
variable to determine what to do. The user can then just set the
variable to get the desired behavior.

However, I tried this and it does not seem to work. I imported the
class, then set the global variable. But the new value of the variable
somehow did not get back into the class methods that need to see it.

Can anyone give me a clue about this? If there is a better way, please
let me know. Thanks.
 
J

James Stroud

Russ said:
I would like to let the user of one of my classes "configure" it by
activating or de-activating a particular behavior (for all instances of
the class).

One way to do this, I figured, is to have a static class variable,
along with a method to set the variable. However, I am stumped as to
how to do that in python. Suggestions welcome.

The next best thing, I figure, is to just use a global variable.
Several "methods" of the class then check the value of the global
variable to determine what to do. The user can then just set the
variable to get the desired behavior.

However, I tried this and it does not seem to work. I imported the
class, then set the global variable. But the new value of the variable
somehow did not get back into the class methods that need to see it.

Can anyone give me a clue about this? If there is a better way, please
let me know. Thanks.

py> class C:
.... doit = 2
....
py> C.doit
2
py> a = C()
py> b = C()
py> c = C()
py> a.doit, b.doit, c.doit
(2, 2, 2)
py> C.doit = 5
py> a.doit, b.doit, c.doit
(5, 5, 5)
py> b.doit = 13
py> a.doit, b.doit, c.doit
(5, 13, 5)

James
 
T

Terry Hancock

I would like to let the user of one of my classes
"configure" it by activating or de-activating a particular
behavior (for all instances of the class).
One way to do this, I figured, is to have a static class
variable, along with a method to set the variable.
However, I am stumped as to how to do that in python.
Suggestions welcome.

Like this?
.... def _plus(self, val):
.... return val
.... def _minus(self, val):
.... return -val
.... switch = _plus
.... def sense(self, value):
.... return self.switch(value)
....-2

Seems to work. Might be more elegant to do that
switch with a method of Switchable. Maybe:

def flipswitch(self, num):
if num == 1:
self.switch = self._plus
else:
self.switch = self._minus

Then you'd switch with, e.g.:

Switchable.flipswitch(1)
The next best thing, I figure, is to just use a global
variable. Several "methods" of the class then check the
value of the global variable to determine what to do. The
user can then just set the variable to get the desired
behavior.
Ick.

However, I tried this and it does not seem to work. I
imported the class, then set the global variable. But the
new value of the variable somehow did not get back into
the class methods that need to see it.

Undoubtedly you shadowed it somehow, but without seeing
the code, I can't guess how.
Can anyone give me a clue about this? If there is a better
way, please let me know. Thanks.

Recommend you stick with the first idea, which
as you see works fine.

Cheers,
Terry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top