简体   繁体   English

如何检查任意类实例是否存在并从另一个模块调用其方法?

[英]How to check if arbitrary class instance exists and call its method from another module?

Lets say I have two modules module1 and module2 and module1 can have objects chair1 , chair2 and chair3 . 可以说我有两个模块module1module2module1可以有对象chair1chair2chair3 I want to check if one of those objects exist from module2 and if it does exist run a method on it also from module2 . 我想检查是否存在来自module2的那些对象之一,以及是否确实存在来自module2 Something like: 就像是:

# module2
if ("chair" + str(i)) in globals():
    globals()["chair" + str(i)].carve() # i = 1, 2 or 3

This obviously won't work because chair1 , chair2 and chair3 are not in globals() of module2 这显然是chair1 ,因为chair1chair2chair3不在module2 globals()

What is the best approach to solve this? 解决此问题的最佳方法是什么?

EDIT: 编辑:

Rob's answer ( hasattr() ) solves the problem of checking if the object exists in the other module. Rob的答案( hasattr() )解决了检查对象是否存在于另一个模块中的问题。 The second part, about running methods on objects I solved by using dictionary (something like: obj_names = {"chair1" : chair1, "chair2" : chair2, "chair3 : chair3} instead of trying to use global() function. As recommended on some other issue this is the most pythonic approach. First part can also be solved with dictionary, like setting obj_names[i] = 0 if the object doesn't exist. 第二部分,关于在我使用字典解决的对象上运行方法(类似: obj_names = {"chair1" : chair1, "chair2" : chair2, "chair3 : chair3}而不是尝试使用global()函数。建议在其他问题上,这是最Python化的方法,也可以使用字典来解决第一部分,例如如果对象不存在, obj_names[i] = 0设置obj_names[i] = 0

Btw, for my particular problem I won't be needing more than 10 chairs so using the dictionary is not a bad option. 顺便说一句,对于我的特殊问题,我不需要超过10把椅子,因此使用字典并不是一个坏选择。 But if the problem was defined so that number of chairs can be any int, than using something like global() would be more logical. 但是,如果定义了问题以使椅子的数量可以是任意整数,那么使用诸如global()类的东西将更合乎逻辑。

Try hasattr() : 试试hasattr()

# module2.py
import module1

if hasattr(module1, "chair1"):
    module1.chair1.carve()

You can use dir : 您可以使用dir

if "chair1" in dir(module1):
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据名称检查 class 的实例是否存在 - How to check if an instance of a class exists based on its name 如何从另一个人的类方法调用或引用实例属性 - How to call or refer to an instance attribute from another's class method 另一个类实例的python类实例方法调用 - python class instance method call from another class instance Python模块pytest-mock如何在一个类调用另一个类实例时测试方法调用参数? - Python module pytest-mock how to test method call arguments when one class call another class instance? 如何从不同模块中另一个类的方法修改特定的类实例? - How can I modify a specific class instance from the method of another class in a different module? 仅当模块存在时,如何从模块导入 class 的实例 - How to import an instance of a class from a module only if it exists Python:如何从另一个模块调用方法? - Python: How to call a method from another module? 如何从另一个类调用一个类的方法? - How to call a method of a class from another class? Python:如何从同一类的类方法调用实例方法 - Python: How to call an instance method from a class method of the same class 从没有 inheritance 的另一个 class 调用方法并创建实例 - Call method from another class without inheritance and creating an instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM