Python不执行任何范例。它给了你自由。Python提供模块级的封装。
如果我有一个模块A和一个模块B具有相同的接口。如何从A继承B并覆盖B提供的一些功能?
发布于 2015-09-23 10:38:57
当您导入Y模块(该模块使用from X import *从模块X导入函数)时,来自X的所有函数都可用,就像在Y中一样(您可以这样做,比如将X的内容粘贴到Y中)。
而且,当您有多个防御,Python将采取最后一个。因此,要重写函数,只需在导入X之后重新定义这些函数。
此外,当您希望在X中使用Y中的原始函数时,您可以添加相应的import X并使用X.functionName()访问它。
module1.py
def foo():
print "this will be overridden"
def bar():
print "this will be preserved"module2.py
from module1 import *
import module1
def foo():
print "This was foo:"
module1.foo()
print "But it was overridden."run.py
import module2
module2.foo()
module2.bar()-> % python run.py
This was foo:
this will be overridden
But it was overridden.
this will be preservedhttps://softwareengineering.stackexchange.com/questions/298019
复制相似问题