所有人。我得到一个错误''AttributeError:‘list’对象在GH_python中没有属性‘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”
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'有人能给我一些建议吗?
发布于 2020-05-10 21:09:56
在这个循环中
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 aufrufenPanelGeo是list,而不是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更改为其他名称。这是因为使用与类名相同的变量名必然会导致混淆。
https://stackoverflow.com/questions/61712509
复制相似问题