“列表”对象没有属性“角度”

2024-09-29 19:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

各位。 在GH_python中,我得到一个错误“AttributeError:“list”对象没有属性“Angle”,我使用“Class”来定义面板。 我从一个图表中得到了三个列表

import numpy as np   
from math import *
from matplotlib import pyplot as plt

PG_i_1 = np.array(PG_i_1)
PG_i_2 = np.array(PG_i_2)
PG_j = np.array(PG_j)
N = len(PG_j) 

然后我设置了一个“类”

class PanelGeo:
    """
     the 
    """
    def __init__(self,P1,P2):
        """
        P1: the first end-point of a panel
        P2: the second end-point of a panel
        """
        self.P1 = P1
        self.P2 = P2

    def Angle(self):
        # Berechne den Winkel (in Rad)
        dx = P2[0]-P1[0]
        dy = P2[1]-P1[1]
        if dx == 0:
            if dy > 0:
                self.alpha = np.pi/2
            else:
                self.alpha = 3*np.pi/2
        else:
            self.alpha = np.arctan(dy/dx)
            if dx > 0:
                if dy >=  0:
                    pass
                else:
                    self.alpha += 2*np.pi
            else:
                self.alpha += np.pi

    def getPanelVectors(self):
        # Berechne Normalenvektor:
        self.n = np.array([np.sin(alpha), -np.cos(alpha),0])
        self.t = np.array([np.cos(alpha), np.sin(alpha),0])
        return n,t

    def getPanelTransformationMatrix(self):
        # Koordinatentransformation von global zu lokal: {P_loc} = [M] * {P_glob}
        # Transformationsmatrix
        self.M = np.matrix([[ np.cos(alpha), np.sin(alpha),0], \
                       [-np.sin(alpha), np.cos(alpha),0],\
                       [0,0,0]])
        return M 

然后我想从这里的“PanelGeo”中得到alpha值

PanelGeo = [PanelGeo(PG_i_1[i],PG_i_2[i]) for i in range(N)]
for i in range(N):
    #Rotationswinkel rechnen
    alpha = PanelGeo.Angle()                 # Funktion aufrufen

我收到了

Traceback (most recent call last):
  File "C:\GH_CPython\PythonFileWritten_3.py", line 59, in <module>
    alpha = PanelGeo.Angle()                 # Funktion aufrufen
AttributeError: 'list' object has no attribute 'Angle'

有人能给我一些建议吗


Tags: inselfalphaifdefnppiarray
1条回答
网友
1楼 · 发布于 2024-09-29 19:32:59

在这个循环中

PanelGeo = [PanelGeo(PG_i_1[i],PG_i_2[i]) for i in range(N)]
for i in range(N):
    #Rotationswinkel rechnen
    alpha = PanelGeo.Angle()                 # Funktion aufrufen

PanelGeolist,而不是PanelGeo的实例。您必须将其更改为:

panelGeos = [PanelGeo(PG_i_1[i],PG_i_2[i]) for i in range(N)]
for i in range(N):
    panelGeo = panelGeos[i]
    #Rotationswinkel rechnen
    alpha = panelGeo.Angle()                 # Funktion aufrufen

更好的是,您可以完全消除对i的需要:

panelGeos = [PanelGeo(PG_i_1[i],PG_i_2[i]) for i in range(N)]
for panelGeo in panelGeos:
    #Rotationswinkel rechnen
    alpha = panelGeo.Angle()                 # Funktion aufrufen

请注意,我是如何将列表的名称从PanelGeo更改为其他名称的。这是因为变量名与类名相同必然会导致混淆

相关问题 更多 >

    热门问题