Resetting Globals With Importing

Posted by what on Stack Overflow See other posts from Stack Overflow or by what
Published on 2010-05-08T05:37:45Z Indexed on 2010/05/08 5:48 UTC
Read the original article Hit count: 189

Filed under:
|

I have this code (Reset.py) that works how I want it to unless I import it.

class Res(object):
    defaults={}
    class NoKey: pass
    def __init__(self):
        for key, values in defaults.items():
            globals()[key]=values
    def add_defaults(key, values):
        Res.defaults[key]=value
    def remove_defaults(key=NoKey, remove_all=False):
        if remove_all:
            defaults={}
        else:
            del defaults[key]

Without importing:

>>> a=54
>>> Res.add_default('a', 3)
>>> Res()
<__main__.Res object at 0x>
>>> a
3
>>> #great! :D

With importing:

>>> a=54
>>> Res.add_default('a', 3)
>>> Res()
<Reset.Res object at 0x>
>>> a
54

This must mean when it is imported it changes the globals() under Reset and not __main__. How can I fix this?

© Stack Overflow or respective owner

Related posts about python-3.x

Related posts about python