Imports in python are static, any solution?

AJ Mayorga aj at xernova.com
Wed Apr 15 08:24:32 CEST 2009


For something like this I generally create a superclass to hold
configuration variables that will change overtime, doing that will save you
from insanity.

Class configVar:

	#set initial values
	Def __init__(self):
		Self.A = 5
		Self.B = 10
		Self.C = 20


Class myMath(configVars):
	def __init__(self):
		pass

	def SubAandB(self):
		return self.A - self.B

	def AddCandB(self):
		return self.C + self.B

	def MultiplyXbyA(self, x):
		return self.A * x


m = myMath()
X = m.SubAandB()
Y = m.AddCandB()
Z = m.MultiplyXbyA(32)

Keeps your vars in a safer easier to handle, debug, and change kinda way
Good luck

AJ

-----Original Message-----
From: python-list-bounces+aj=xernova.com at python.org
[mailto:python-list-bounces+aj=xernova.com at python.org] On Behalf Of David
Stanek
Sent: Monday, April 13, 2009 12:12 PM
To: Ravi
Cc: python-list at python.org
Subject: Re: Imports in python are static, any solution?

On Mon, Apr 13, 2009 at 11:59 AM, Ravi <ra.ravi.rav at gmail.com> wrote:
> foo.py :
>
>    i = 10
>
>   def fi():
>      global i
>      i = 99
>
> bar.py :
>
>    import foo
>    from foo import i
>
>    print i, foo.i
>    foo.fi()
>    print i, foo.i
>
> This is problematic. Well I want i to change with foo.fi() .

Why not only import foo and using foo.i? In fi() when you set i = 99
you are creating a new object called i in foo's namespace.


-- 
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek
--
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list