Scoped change of a global variable

Steven Majewski sdm7g at Virginia.EDU
Wed Jan 2 18:46:42 EST 2002


[ To follow myself up ] ... but the more Pythonic solution
would probably be to avoid using global values for defaults
and to use a class instead:


>>> class NameSpace:
...     default = 1
...     def __init__( self, **kws ):
...             for key,value in kws.items():
...                     setattr( self, key, value )
...     def __call__( self, n ):
...             print n + self.default
...
>>> NameSpace.default
1
>>> NameSpace()(2)
3
>>> NameSpace( default=100 )(2)
102
>>> NameSpace.default
1
>>>



-- Steve Majewski






More information about the Python-list mailing list