2015-11-29 36 views
0

我想从导入模块中的类向当前命名空间分配方法(更多一次),我想在“当前”命名空间中执行“分配进程”,但不是来自进口模块。我怎样才能做到这一点?将导入模块中类的方法分配给当前命名空间

该文件是从另一个输入:

# File mylib.py 

class MyLib(): 
    def hello1(self, text): 
     print('hello1: %s' % text) 

    def goodbye1(self, text): 
     print('goodbye1: %s' % text) 

    def hello2(self, text): 
     print('hello2: %s' % text) 

    def goodbye2(self, text): 
     print('goodbye2: %s' % text) 

    def assign1(self): 
     pass 
     # This should assign self.hello1() and self.goodbye1() 
     # to "parent" namespace as hello() and goodbye() 

    def assign2(self): 
     pass 
     # Similar behaviour to self.assign1() 

这个文件是“主”之一。

# File myscript.py 

import mylib 

l = mylib.MyLib() 
l.assign1() 

# After this I would like to have MyLib.hello1() assigned to hello() 
# and MyLib.goodbye1() to goodbye() 

hello('hi') 
goodbye('see you') 

l.assign2() 
# After this I would like to have MyLib.hello2() assigned to hello() 
# and MyLib.goodbye2() to goodbye() 

hello('hi') 
goodbye('see you') 

到目前为止,我试过globals()没有成功,结果发现__builtins__可能会奏效,但似乎不是因为污染__builtins__命名空间不属于那里内容的正确方法。

我不想要什么:

# In the "current" namespace 
hello = l.hello1 
goodbye = l.goodbye1 

# Instead I want 

l.assign1() 
# Now hello() and goodbye() are mapped to l.hello1() and l.goodbye1() 
# "automatically" - the assign process was done in l.assign1(), 
# not here in the "current" namespace. 

感谢您的帮助。

回答

1

assign1采取命名空间要分配

def assign1(self, namespace): 
     namespace['hello'] = self.hello1 
     namespace['goodbye'] = self.goodbye1 

现在呼叫者会

import mylib 
l = mylib.MyLib() 
l.assign1(globals()) 
+0

感谢大家谁张贴解答。这个解决了我的问题。我修改了一下 - 我将globals()传递给MyLib .__ init __(),所以我不必将它传递给assign1()或assign2() – dwich

0

您可以将变量放入模块的全局名称空间globals()。这是否足够......?

文件:submodule.py

class Bar(object): 
    def __init__(self, baz="Hi!"): 
     self.baz = baz 

    def foo(self): 
     print("foo %s" % self.baz) 

    def assign(self): 
     globals()["foo"] = self.foo 

主要脚本:

import submodule 

bar = submodule.Bar() 
bar2 = submodule.Bar("Bye!") 

bar.assign() 
submodule.foo() 

bar2.assign() 
submodule.foo() 

输出:

foo Hi! 
foo Bye! 
0

我认为这将是更好的让你不调整你的代码必须这样做,也许这样:

# File mylib.py 

class MyLib(): 
    #... 

    def assign1(self): 
     return hello1, goodbye1 

    #likewise for assign2 

而且主脚本...

# File myscript.py 

import mylib 

l = mylib.MyLib() 
hello, goodbye = l.assign1() 

如果你真的改变什么是父命名空间,那么也许有办法做到这一点如下:

# File mylib.py 

class MyLib(): 
    def __init__(self, funcs): #funcs should be a list 
     self.funcs = funcs 

    #... 

    def assign1(self): 
     # This should assign self.hello1() and self.goodbye1() 
     # to "parent" namespace as hello() and goodbye() 

     self.funcs[0].__code__ = self.hello1.__code__ 
     self.funcs[1].__code__ = self.goodbye1.__code__ 

    #likewise for assign2 

而且主脚本...

# File myscript.py 

import mylib 

hello = lambda s:s 
goodbye = lambda s:s 

l = mylib.MyLib([hello, goodbye]) 
l.assign1() 

但是,我想说,这让我觉得......“脏”。就像,这不是很干净或Pythonic。对于未来/更大的项目,请使用第一种方法。