您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python中的嵌套try语句?

python中的嵌套try语句?

也许您可以尝试这样的事情:

def call_attrs(obj, attrs_list, *args):
    for attr in attrs_list:
        if hasattr(obj, attr):
            bound_method = getattr(obj, attr)
            return bound_method(*args)

    raise AttributeError

您可以这样称呼它:

call_attrs(a, ['method1', 'method2', 'method3'])

这将尝试按它们在列表中的顺序调用这些方法。如果您想传递任何参数,可以像下面这样在列表之后传递它们:

call_attrs(a, ['method1', 'method2', 'method3'], arg1, arg2)
python 2022/1/1 18:33:30 有202人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶