Scoped change of a global variable

Corrado Gioannini gioco at nekhem.com
Wed Jan 2 05:53:14 EST 2002


On Wed, Jan 02, 2002 at 11:29:45AM +0000, Luigi Ballabio wrote:
[...snip...] 

You could define a class like:

precision = 2
class Add:
    def __init__(self):
        self.precision = precision
    def add(self, x, y):
	...
    def setPrecision(self, n):
        self.precision = n
    def restorePrecision(self):
        self.precision = precision

and then simply:

a = Add()
a.add(1.0, 0.3456)
a.setPrecision(3)
a.add(1.0, 0.3456)
a.restorePrecision()


Of course you can use a class variable instead of a global variable:

class Add:
    default_precision = 2
    ... (same as above with self.default_precision instead of precision)


This if you really don't want to use parameters...

precision = 2
def add(x, y, z=precision):
    ...


Hope this helps,
Corrado
-- 
Corrado Gioannini
<gioco at nekhem.com>

"Thought is only a flash between two long nights,
                                         but this flash is everything."
                                                          (H. Poincaré)
		      




More information about the Python-list mailing list