我有一个实现两个(非正式)接口的多态对象数组。我希望能够通过以下方面的反射来区分它们:
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') {
# ...
我很难说出语法何时会尝试调用子例程以及何时返回子例程引用。我对包符号表 ATM 不太熟悉,我只是想破解一些东西。:-)
提前致谢!