Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在Python2.7中,如何包装类实例方法或用try/except块修饰它?_Python 2.7_Wrapper_Decorator_Try Except_Instance Methods - Fatal编程技术网

Python 2.7 在Python2.7中,如何包装类实例方法或用try/except块修饰它?

Python 2.7 在Python2.7中,如何包装类实例方法或用try/except块修饰它?,python-2.7,wrapper,decorator,try-except,instance-methods,Python 2.7,Wrapper,Decorator,Try Except,Instance Methods,我在一系列类方法中重复了一组冗长的try/except1/except2/etc块,这些方法的不同之处在于在外部类实例上调用的外部类方法。下面是一个简化版本(实际上我正在处理4个异常和8个方法,它们只因调用的实例方法不同而不同): 我一直在尝试各种方法来压缩这段代码,通过使用嵌套函数、装饰器等将这两条语句包装在try中:代码的try部分,但似乎失败了,因为我在翻译其他示例时遇到了问题,原因是:1)我正在创建一个类实例,该类实例需要稍后在一个except块中使用;2)我正在调用一个实例方法;3)我

我在一系列类方法中重复了一组冗长的try/except1/except2/etc块,这些方法的不同之处在于在外部类实例上调用的外部类方法。下面是一个简化版本(实际上我正在处理4个异常和8个方法,它们只因调用的实例方法不同而不同):

我一直在尝试各种方法来压缩这段代码,通过使用嵌套函数、装饰器等将这两条语句包装在try中:代码的try部分,但似乎失败了,因为我在翻译其他示例时遇到了问题,原因是:1)我正在创建一个类实例,该类实例需要稍后在一个except块中使用;2)我正在调用一个实例方法;3)我需要返回实例方法的结果


使用工具、描述符或任何其他方法是否可以实现这一点?我目前有一个笨重的实现,带有一个扩展的if/elif块,它根据我在包装函数中使用的整数代码选择实例方法,但我认为必须有一种更优雅的方法。我对Python比较陌生,也不知道……

您可以使用函数工厂(即,返回函数的函数)

make_方法
作为字符串传递外部方法名。 用于(在
method
中)从
x
中获取给定字符串
methname
的实际方法。
getattr(x,'foo')
相当于
x.foo

def方法(self,*args)
中的
*
告诉Python
method
可以接受任意数量的位置参数。 在
method
中,
args
是一个元组。
y=getattr(x,methname)(*args)
中的
*
告诉Python将
args
中的元素作为单个参数传递给
getattr(x,methname)
返回的方法。中解释了
*
解包操作员,也在中进行了说明

class MyClass(object):
    def __init__(self):
        self.arg = 'foo'

    def method1(self, arg1):
        err = -1
        y = None
        try:
            x = AnOutsideClass(self.arg)     # Creates a class instance of an imported class
            y = x.outsideclassmethod1(arg1)  # Calls an instance method that returns another different class instance
        except MyException1:
            x.dosomething() # Needed to handle error
        except MyException2:
            err = 0
        finally:
            del x
        return y, err

    def method2(self, arg1, arg2, arg3):
        err = -1
        y = None
        try:
            x = AnOutsideClass(self.arg)
            y = x.outsideclassmethod2(arg1, arg2, arg3)  # This is the only thing changed
                                                         # A different method with different argument requirements
        except MyException1:
            x.dosomething()
        except MyException2:
            err = 0
        finally:
            del x
        return y, err

    def method3 ...
def make_method(methname):
    def method(self, *args):
        err = -1
        y = None
        try:
            x = AnOutsideClass(self.arg)     # Creates a class instance of an imported class
            y = getattr(x, methname)(*args)  # Calls an instance method that returns another different class instance
        except MyException1:
            x.dosomething() # Needed to handle error
        except MyException2:
            err = 0
        finally:
            del x
        return y, err
    return method

class MyClass(object):
    def __init__(self):
        self.arg = 'foo'
    method1 = make_method('outsideclassmethod1')
    method2 = make_method('outsideclassmethod2')