Skip to main content
 首页 » 编程设计

python之如何获取有关函数的信息并调用它

2024年08月12日29soundcode

我想创建一个函数来检测给定实例是否存在方法,可以传入哪些参数,然后使用适当的参数调用该方法。我是新手,我不知道该怎么做:(

请您参考如下方法:

试试 hasattr

>>> help(hasattr) 
Help on built-in function hasattr in module __builtin__: 
 
hasattr(...) 
    hasattr(object, name) -> bool 
 
    Return whether the object has an attribute with the given name. 
    (This is done by calling getattr(object, name) and catching exceptions.) 

要了解更高级的内省(introspection),请阅读inspect 模块。

但首先,请告诉我们您为什么需要这个。存在更好方法的可能性为 99%...