Mucking with the calling scripts namespace (For a good reason, honest!)

Peter Otten __peter__ at web.de
Wed Aug 4 03:07:12 EDT 2004


Christopher T King wrote:

> setattr().  Something as simple as the following would suffice:
> 
>  class objifier(object):
>       def __init__(self,d):
>           self.__dict__ = d
> 
>  __main__ = objifier(globals())

Or something even simpler:

import __main__
 
> Then you do stuff like:
> 
>>>> __main__.b = 6
>>>> b
> 6
>>>> b = 20
>>>> __main__.b
> 20
>>>> getattr(__main__,"b")
> 20
>>>> setattr(__main__,"b",6)
>>>> b
> 6

Peter




More information about the Python-list mailing list