Scoped change of a global variable

Alex Martelli aleax at
Thu Jan 3 09:07:59 CET 2002

"Steven Majewski" <sdm7g at Virginia.EDU> wrote in message
news:mailman.1010015222.20515.python-list at
> [ 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 )

In a classic-classes environment, I'd rephrase this __init__ as
the somewhat more compact and faster:

    def __init__(self, **kwds):

Admittedly, your setattr-based approach is more general (e.g. it
works if NameSpace is mixin-inherited from together with some
other mixin that defines __setattr__, while the __dict__.update
approach would bypass __setattr__ in this case).


More information about the Python-list mailing list