我有一个包含多态对象的数组,这些对象实现了两个(非正式的)接口。我希望能够通过反射来区分它们,例如:
if (hasattr(obj, 'some_method')) {
# `some_method` is only implemented by one interface.
# Now I can use the appropriate dispatch semantics.
} else {
# This must be the other interface.
# Use the alternative dispatch semantics.
}
也许像这样的东西可以工作吗?
if (*ref(obj)::'some_method') {
# ...
我很难分辨语法何时尝试调用子例程以及何时返回子例程引用。我对软件包符号表不太熟悉,只是试图进行一些简单的操作。:-)
提前感谢!
can
的命令有点过于严格。这样做有很好而且重要的原因。 - darch