Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 当调用了另一个类方法时,如何使类方法执行_Python_Python 3.x - Fatal编程技术网

Python 当调用了另一个类方法时,如何使类方法执行

Python 当调用了另一个类方法时,如何使类方法执行,python,python-3.x,Python,Python 3.x,我正在用python编写一个图像预处理类 出于调试目的,我有兴趣在每次调用类方法时运行调试方法,该类方法将根据方法调用或init方法签名中指定的参数保存/显示输入和输出图像 有没有一种方法可以让这样的函数在不需要调用每个新方法的情况下执行 目前,我已经编写了一个Show方法,并且在我的类的每个方法上都调用了它。我很快就会编写一个类似的保存文件方法 def Show(self, image, viewOutput=True, title=None): if self.BOOL_StepBy

我正在用python编写一个图像预处理类

出于调试目的,我有兴趣在每次调用类方法时运行调试方法,该类方法将根据方法调用或init方法签名中指定的参数保存/显示输入和输出图像

有没有一种方法可以让这样的函数在不需要调用每个新方法的情况下执行

目前,我已经编写了一个Show方法,并且在我的类的每个方法上都调用了它。我很快就会编写一个类似的保存文件方法

def Show(self, image, viewOutput=True, title=None):
    if self.BOOL_StepByStepViewOutput or viewOutput:
        fig = plt.figure(figsize=(12, 8), facecolor='w', edgecolor='k')
        plt.imshow(image, cmap='gray')
        if title:
            plt.title(title)
        plt.show() 

def ExamplePreprocessingFunction (self , InputImage  , BoolViewOutput , BoolViewInput): 
    self.show(InputImage , BoolViewInput , 'Input Image ,      ExamplePreprocessingFunction') 
    OutputImage=#Do some pre-processing staff   
    self.show (OutputImage , BoolViewOutput , 'Output Image Pre-Processing Function') 
我想有一个功能,将自动显示或不显示保存或不保存输入和输出图像的参数为基础的指定通过初始化方法或更好地通过预处理方法来执行。
在所有的实现中,我都没有过度删除输入图像

装饰师的良好用例:

from functools import wraps

def log_this(func):
    @wraps(func)
    def inner(self, *args, **kwargs):
        self.log('Before calling', func.__name__)
        r_value = func(self, *args, **kwargs)
        self.log('After calling', func.__name__)
        return r_value
    return inner

class Foo:
    @log_this
    def bar(self):
        print('in bar')

    def log(self, *args):
        print(*args)

f = Foo()
f.bar()
输出

Before calling bar
in bar
After calling bar