Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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_Inheritance_Instance - Fatal编程技术网

Python 如何在同一类的前一个实例上调用实例?

Python 如何在同一类的前一个实例上调用实例?,python,inheritance,instance,Python,Inheritance,Instance,如果这个问题有一个明显的解决方案,或者是重复的,我提前道歉 我的课程如下: class Kernel(object): """ creates kernels with the necessary input data """ def __init__(self, Amplitude, random = None): self.Amplitude = Amplitude self.random = random if random

如果这个问题有一个明显的解决方案,或者是重复的,我提前道歉

我的课程如下:

class Kernel(object):
    """ creates kernels with the necessary input data """
    def __init__(self, Amplitude, random = None):
        self.Amplitude = Amplitude
        self.random = random
        if random != None:
            self.dims = list(random.shape)

    def Gaussian(self, X, Y, sigmaX, sigmaY, muX=0.0, muY=0.0):
        """ return a 2 dimensional Gaussian kernel """
        kernel = np.zeros([X, Y])
        theta = [self.Amplitude, muX, muY, sigmaX, sigmaY]
        for i in range(X):
            for j in range(Y):
                kernel[i][j] = integrate.dblquad(lambda x, y: G2(x + float(i) - (X-1.0)/2.0, \
                                                 y + float(j) - (Y-1.0)/2.0, theta), \
                                                 -0.5, 0.5, lambda y: -0.5, lambda y: 0.5)[0]
        return kernel
它基本上只是创建了一堆卷积内核(我只包含了第一个)

我想向这个类添加一个实例(方法?),这样我就可以使用

conv = Kernel(1.5)
conv.Gaussian(9, 9, 2, 2).kershow()
并使用Matplotlib弹出数组。我知道如何编写此实例并使用Matplotlib绘制它,但我不知道如何编写此类,以便对于我希望具有此附加功能的每个方法(即
.kershow()
),我可以这样调用它


我想我可以用装饰师?但我以前从未用过。我该怎么做?

以下是我该怎么做:

class Kernel(object):

    def __init__ ...

    def Gaussian(...):
        self.kernel = ...
        ...
        return self  # not kernel

    def kershow(self):
        do_stuff_with(self.kernel)
基本上,
Gaussian
方法不会返回numpy数组,它只是将其存储在
Kernel
对象中,以便在类的其他地方使用。尤其是
kershow
现在可以使用它了。
返回self
是可选的,但允许在编写时使用所需的接口类型

conv.Gaussian(9, 9, 2, 2).kershow()
而不是

conv.Gaussian(9, 9, 2, 2)
conv.kershow()

您要查找的对象的名称是函数或

在Python中,字符串就是一个很好的例子。因为字符串是不可变的,所以每个string方法都返回一个新字符串。因此,您可以对返回值调用string方法,而不是存储中间值。例如:

lower = '       THIS IS MY NAME: WAYNE      '.lower()
without_left_padding = lower.lstrip()
without_right_padding = without_left_padding.rstrip()
title_cased = without_right_padding.title()
相反,你可以写:

title_cased = '       THIS IS MY NAME: WAYNE      '.lower().lstrip().rstrip().title()
当然,您只需执行
.strip().title()
,但这是一个示例

因此,如果您想要一个
.kernshow()
选项,那么无论返回什么,都需要包含该方法。在您的例子中,numpy数组没有
.kernshow
方法,因此您需要返回一些可以返回的方法

您的选择主要包括:

  • numpy数组的一个子类
  • 包装numpy数组的类
我不确定子类化numpy数组涉及到什么,所以我将以后者为例。您可以使用内核类,也可以创建第二个类

Alex提供了一个使用内核类的示例,但是您也可以使用另一个类,如下所示:

class KernelPlotter(object):
    def __init__(self, kernel):
        self.kernel = kernel

    def kernshow(self):
        # do the plotting here
然后,您将基本上遵循现有的代码,但不是
returnkernel
,而是
returnkernelplotter(kernel)

您选择哪一个选项实际上取决于对您的特定问题领域有意义的内容


还有另一个姐妹函数链接称为a,基本上是函数链接,但目标是使界面读起来像英语一样。例如,您可能有如下内容:

Kernel(with_amplitude=1.5).create_gaussian(with_x=9, and_y=9, and_sigma_x=2, and_sigma_y=2).show_plot()

虽然这样编写代码时显然会出现这种情况。

它看起来像是
Gaussian
函数的
kernel
对象是一个numpy数组,该函数返回该数组。numpy阵列是否有
kershow
方法?如果不是,您必须将它添加到numpy源代码中(我假设它是开源的),或者在类中创建一个函数来接受这样一个数组作为参数。非常感谢。