python - 如何在 python 的另一个文件下调用另一个类中的方法?
问题描述
如何从我的python文件中调用同一包下另一个文件中另一个类中的方法?
解决方案
您首先需要将文件(取决于您的目录结构)导入当前脚本,创建该类的实例,然后在该类中调用该方法;
#Import your other file
import other_file
#Create an instance of the object where your method is
my_obj = NewClass()
#Call the method directly
my_obj.some_method()
您还可以尝试在方法中添加 @staticmethod 以使其可直接调用:
class NewClass:
@staticmethod
def some_method():
print 'Hello'
#call staticmethod add directly
#without declaring instance and accessing class variables
NewClass.some_method()
推荐阅读
- mysql - 在 mysql db 中创建表,错误 1064 (42000)?
- vb.net - 如何从数据库中的 3 个表中收集自定义数据
- android - SwipeRefreshLayout 是否与具有多个 viewType 的适配器兼容?
- geojson - 确定一个位置的描笼涯
- oracle - 更新initcap和upper记录的代码
- python - 如何在 Matplotlib 图中删除多余的水平线
- javascript - Nuxtjs handler.call 不是函数
- c# - 重构代码以允许对 HttpClient 进行单元测试
- latex - 将 esttab 与 ttest 一起使用
- python - Python 在 __init__ 中为包和函数创建 if elif