Python类和继承。调用超级方法返回错误
我有以下python代码。代码分析用于放大处理的数字信号,但是由于某种原因,我在将继承概念应用于此方面遇到困难 我有一个父类和一个子类,它将父类设置为一个参数,以在子类中继承其属性。 但是当我尝试使用OOP继承原则访问父类中的方法时,我得到了一个错误。我最初尝试扩展该类而不是重载它,但是当我尝试访问扩展父类中的dofilter()方法时,它会抛出一个错误。但是,如果我尝试在子类中重载dofilter()方法,然后使用“super”从父类调用它,则没有错误,但返回“NaN”,这显然意味着我仍然在做错误的事情。我知道数据正在被传递到对象,所以应该没有理由返回NaN。这让我现在有点陷入僵局有人能解释一下为什么这不起作用吗? 为了更好地描述问题,我尝试了什么/我的总结脚本:Python类和继承。调用超级方法返回错误,python,class,oop,object,inheritance,Python,Class,Oop,Object,Inheritance,我有以下python代码。代码分析用于放大处理的数字信号,但是由于某种原因,我在将继承概念应用于此方面遇到困难 我有一个父类和一个子类,它将父类设置为一个参数,以在子类中继承其属性。 但是当我尝试使用OOP继承原则访问父类中的方法时,我得到了一个错误。我最初尝试扩展该类而不是重载它,但是当我尝试访问扩展父类中的dofilter()方法时,它会抛出一个错误。但是,如果我尝试在子类中重载dofilter()方法,然后使用“super”从父类调用它,则没有错误,但返回“NaN”,这显然意味着我仍然在做
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import numpy as np
import matplotlib.pyplot as plt
# =============================================================================
# CLASSES
# =============================================================================
class fir_filter:
def __init__(self, coefficients):
self.coefficients = coefficients
self.buffer = np.zeros(taps)
def dofilter(self, value, offset):
result = 0
#Splice coefficients and buffer arrays into smaller arrays
buffer_newest = self.buffer[0:offset+1]
buffer_oldest = self.buffer[offset+1:taps]
coefficients1 = self.coefficients[0:offset+1]
coefficients2 = self.coefficients[offset+1:taps]
#Accumulate
self.buffer[offset] = value
for i in range(len(coefficients1)):
result += coefficients1[i]*buffer_newest[offset-1-i]
for i in range(len(coefficients2), 0, -1):
result += coefficients2[len(coefficients2)-i] * buffer_oldest[i-1]
return result
class matched_filter(fir_filter):
def __init__(self,coefficients):
self.coefficients = coefficients
def dofilter(self,value,offset):
super().dofilter(value, offset)
return result
########################################
#START OF MAIN SCRIPT
########################################
#...
#REMOVED- import data files, create vars, perform various calculations.
########################################
#... RESUME CODE pertinent variables here
m_wavelet = (2/(np.sqrt(3*a*(np.pi**(1/4)))))*(1-((n/a)**2))*np.exp((-n**2)/(2*(a**2)))
m_wavelet = m_wavelet [::-1]
result = np.zeros(l)
offset = 0
plt.figure(6)
plt.plot(m_wavelet)
plt.plot(ecg_3[8100:8800]/len(ecg_3)/300)
########################################
#instantiated object here, no errors thrown
########################################
new_filter = matched_filter(m_wavelet)
########################################
#issue below
########################################
for k in range(len(ecg_3)):
result[k] = new_filter.dofilter(ecg_3[k], offset) #<- Every item in the array is "Nan"
offset += 1
if (offset == 2000):
offset = 0
########################################
#More removed code/end of script
########################################
儿童班:
代码和问题:
如何访问“fir\u filter”中的“dofilter()”,它是在“matched\u filter”中继承的?
如有任何见解,将不胜感激。如果需要,我可以提供更多的代码。您应该会得到一个
名称错误
,因为结果
未在匹配的\u filter.dofilter
中定义。您可以简单地编写return super().dofilter(value,offset)
,但如果您就是这么做的,那么首先就没有理由重写defilter
。为什么存在匹配的\u filter
?什么是fir_filter
还没有做的matched\u filter.\uuuu init\uuuu
应调用fir\u filter.\uuuu init\uuuu
以确保正确定义缓冲区
属性,但在这样做之后,matched\u filter.\uuuu init\uuu
无需执行任何其他操作matched_filter
是一个只将所有内容委托给其父级的类;它不提供任何附加功能。我需要创建一个新类,访问该父类,并通过我的新子类利用dofilter()。谢谢你指出回报,非常感谢。您提到可以在不覆盖dofilter()的情况下访问dofilter()。当我尝试这样做时,它抛出了一个错误。我做错了什么?如何在不覆盖的情况下访问它?非常感谢您的快速回复!继承的全部要点是child().foo()
可以访问parent.foo
,即使child
没有定义foo
。如果一个类没有做父类已经做过的事情,那么为什么要创建一个新类呢?谢谢你的回复。这是一个更大的任务的一部分,我需要“创建一个新类”来利用父类。我知道这是多余的。。然而,我试图直接访问父类,它抛出了一个错误,这就是为什么我最终试图覆盖并调用super方法。你能概述一下我如何能直接访问它,这样我就可以看到我做错了什么吗?
class fir_filter:
def __init__(self, coefficients):
self.coefficients = coefficients
self.buffer = np.zeros(taps)
def dofilter(self, value, offset):
result = 0
#Splice coefficients and buffer arrays into smaller arrays
buffer_newest = self.buffer[0:offset+1]
buffer_oldest = self.buffer[offset+1:taps]
coefficients1 = self.coefficients[0:offset+1]
coefficients2 = self.coefficients[offset+1:taps]
#Accumulate
self.buffer[offset] = value
for i in range(len(coefficients1)):
result += coefficients1[i]*buffer_newest[offset-1-i]
for i in range(len(coefficients2), 0, -1):
result += coefficients2[len(coefficients2)-i] * buffer_oldest[i-1]
return result
class matched_filter(fir_filter):
def __init__(self,coefficients):
self.coefficients = coefficients
def dofilter(self,value,offset):
super().dofilter(value, offset)
return result
new_filter = matched_filter(m_wavelet)
for k in range(len(ecg_3)):
result[k] = new_filter.dofilter(ecg_3[k], offset)