python - 如何从 foo.py 访问 show()?
问题描述
foo.py
def show():
print("x is from foo")
酒吧.py
def show():
print("x is from bar")
foobar.py
from foo import *
from bar import *
show()
我想show()
从foo.py
.
我的尝试:
foobar.py
from foo import *
from bar import *
#Don't change the above two lines. Keep them as it is.
import gc
show() #Calls show() from bar.py
found = []
for obj in gc.get_objects():
if type(obj) == type(show):
found.append(obj)
print(found)
如何show()
访问foo.py
?
解决方案
好吧,由于您导入文件的方式以及它们都具有相同的函数名 ("show"),因此只能访问"bar.py"中的函数,从而覆盖"foo.py" 的函数,因为“ foo.py”首先被导入。
因此,您需要更改导入方法。代码应如下所示:
import foo
import bar
import gc
bar.show() #Calls show() from bar.py
foo.show() #Calls show() from foo.py
found = []
for obj in gc.get_objects():
if type(obj) == type(show):
found.append(obj)
print(found)
小心
推荐阅读
- machine-learning - 在我的工作中使用 NLP 时,一个 Bag of Word 被两个级别使用,结果分配给了 Single Level。如何克服这个问题?
- reactjs - React Kendo Treeview 滚动到项目
- java - 如何使用 HashMap 填充 MySQL 数据库
- python - python中具有一个功能的因素
- hive - 当元素可以分配到多个组时,如何计算组中的元素
- azure - Azure - 如何列出 Azure 中的所有 Databricks 工作区
- python - 当另一个 Microsoft Word 文档打开时,Python os.remove() 无法删除 .doc 文件
- c# - 在 SSIS 使用脚本任务中删除逗号分隔文件 csv 中的逗号
- cartopy - Cartopy 非零中心经度被轮廓扭曲
- r - 基于跨因素的条件语句从数据集中删除行