473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

"configuring" a class

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.

Feb 23 '06 #1
2 1067
Russ wrote:
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
Feb 23 '06 #2
On 22 Feb 2006 17:28:35 -0800
"Russ" <uy*******@sneakemail.com> wrote:
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?
class Switchable(object): .... def _plus(self, val):
.... return val
.... def _minus(self, val):
.... return -val
.... switch = _plus
.... def sense(self, value):
.... return self.switch(value)
.... a = Switchable()
b = Switchable()
a.sense(2) 2 b.sense(3) 3 Switchable.switch = Switchable._minus
a.sense(3) -3 b.sense(2)

-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

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Feb 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Chris Krasnichuk | last post by:
hello, Does anyone know how I make php work on "my" computer? I made a mistake in my last post so I fixed it here. Chris
1
by: qwejohn | last post by:
Hello, I had posted this question in the twisted mailing list but did not got a solution ; I hope that the python Gurus of this forum can help me a bit. I am trying the exmaple in the python...
7
by: newseater | last post by:
Hello. I need to be able to control how objects are created. Sometimes when creating an object, i want to reuse another object instead. I've searched for factory method implementations and...
6
by: Matt Adams | last post by:
I want define for a couple of words inside a longer text with different font specifications. I declared it like: ....text before.... <FONT class=aaa> these text should get other attributes...
3
by: Steve Lutz | last post by:
Hi All, I have a Windows Service that runs well. The service hosts a remote object. The purpose of the object is so that I can "peak" into the service to see what it's doing. I wrote a small...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
4
by: chris.huh | last post by:
This may not be the best place to put this but i have set up apache as a testing server on my local machine. Whenever i try to use a formmail.php script i downloaded is always come sup saying that...
2
by: ezageza | last post by:
Hi, does anyone know whats going on or how to fix my problem with windows media player 10. When i'm finished using it and am shutting it down it brings up a message saying something like 'windows...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.