消失的123 2021-08-22 10:06 采纳率: 100%
浏览 671
已结题

Python 当在类中写递归函数的时候出现AttributeError: 'Solution' object has no attribute 'hepler',但是明明类中有这个函数


import math

import pandas as pd
class Point:
    def __init__(self,id,x,y,z,type):
        self.id=id
        self.x=x
        self.y=y
        self.z=z
        self.type=type  #为1是垂直校正点,为0是水平校正点
        self.visited=False
        return
    def __str__(self):
        return ("id: {}; x: {}; y: {}; z: {}; type: {}; visited: {}".format(self.id, self.x, self.y,self.z,self.type,self.visited))

class Solution:
    def __init__(self):
        self.point=[]
        self.path=[]
        self.pointA=None
        self.pointB=None
        self.curr = self.pointA
        self.pvv=25
        self.pvh=15
        self.phv=20
        self.phh=25
        self.p5=30
        self.pa=0.001
        self.path.append(self.pointA)


    def distance(self,a,b):
        return math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z))

    def check(self,target,b,v,h):
        err_v=v+self.distance(target,b)*self.pa
        err_h=h+self.distance(target,b)*self.pa
        if target.type == 1:  # 垂直校正点
            if err_v <= self.pvv and err_h <= self.pvh:
               return True
        else:  # 水平校正点
            if err_v <= self.phv and err_h <= self.phh:
               return True
        return False


    def init(self):
        data1=pd.read_excel("附件1:数据集1-终稿.xlsx")
        row=data1.shape[0]
        for i in range(row):
            if(data1.iloc[i,4] =="A 点"):
                self.pointA=Point(data1.iloc[i,0],data1.iloc[i,1],data1.iloc[i,2],data1.iloc[i,3],data1.iloc[i,4])
                continue
            if (data1.iloc[i, 4] == "B点"):
                self.pointB = Point(data1.iloc[i, 0], data1.iloc[i, 1], data1.iloc[i, 2], data1.iloc[i, 3], data1.iloc[i, 4])
                continue
            temp=Point(data1.iloc[i,0],data1.iloc[i,1],data1.iloc[i,2],data1.iloc[i,3],data1.iloc[i,4])
            self.point.append(temp)

    def start(self):
        target=self.pointA
        pre_v=0
        pre_h=0
        self.helper(target, self.pointB, pre_v,pre_h)
        for i in s.path:
            print(i)

    def helper(self,target,aim,pre_v,pre_h):
        if self.check(target,self.pointB,pre_v,pre_h)==True:
            return True
        potential=[]
        for temp in self.point:
            if temp.visited == False:
                d = self.distance(temp,target)
                err_v = pre_v + d * self.pa
                err_h = pre_h + d * self.pa
                if temp.type==1: # 垂直校正点
                    if (err_v<=self.pvv) and (err_h<=self.pvh):
                        potential.append(temp)
                else:#水平校正点
                    if (err_v<=self.phv) and (err_h<=self.phh):
                        potential.append(temp)
        if len(potential)==0:
            return False
        for test in potential:
            target1=test
            self.point.remove(target1)
            target1.visited=True
            self.path.append(target1)
            if target1.type==1:
                pre_v=0
                pre_h=err_h
                if not self.hepler(target1, aim, pre_v, pre_h):
                    self.point.append(target1)
                    target1.visited = False
                    self.path.remove(target1)
            else:
                pre_v=err_v
                pre_h=0
                if not self.hepler(self,target1, aim, pre_v, pre_h):
                    self.point.append(target1)
                    target1.visited = False
                    self.path.remove(target1)

s=Solution()
s.init()
s.start()

运行结果:
Traceback (most recent call last):
if not self.hepler(self,target1, aim, pre_v, pre_h):
AttributeError: 'Solution' object has no attribute 'hepler'

  • 写回答

2条回答 默认 最新

  • 关注

    你定义的是helper
    def helper(self,target,aim,pre_v,pre_h):
    调用的时候写的hepler
    if not self.hepler(self,target1, aim, pre_v, pre_h):

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月22日
  • 已采纳回答 8月22日
  • 创建了问题 8月22日

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的