Skip to main content
 首页 » 编程设计

python之类型错误 : 'float' object is not iterable; occurs when calling class from another file

2025年05月04日22birdshome

这是我第一次在 stackoverflow 上发帖,非常感谢您的帮助。我对 python 比较陌生,这是我第一次从事涉及编码的个人项目。该文件的最终目的是创建一个类来根据输入绘制电机曲线。我目前的主文件是:

import motor 
import plot 
 
if __name__ == '__main__': 
    try: 
        x = motor.motor(6.5,0.275,0.0343,1047,6) 
        print(x.Imax,x.Imin,x.Tmax,x.Wmax,x.V,x.Kv(),x.Kt())     
        a = map(1047,0.0343,x.kt,0.275,6) 
        #a.plot() 
    except KeyboardInterrupt: 
        print('Quitting') 

motor.py 正在按预期工作。当代码到达 a = map(1047,0.343,x.kt,0.275,6) 时,我收到错误:TypeError:'float' object is not iterable。 map 是我创建的一个类,由 plot.py 提供。目前代码如下:

class map: 
    def __init__(self,Wmax,Tmax,kt,Imin,V):  
        #Defines Inputs 
        self.Wmax = Wmax 
        self.Tmax = Tmax 
        self.kt = kt 
    self.Imin = Imin 
        self.V = V 
 
        #Defines motor opperating range 
        self.Wrange = np.arange(0,int(self.Wmax),1)  
 
        #Defines Torque as a function of angular velocity 
        #self.T_function = self.Tmax*(1-self.Wrange/self.Wmax) 
 
        #Defines mechanical power as a functionof angular velocity 
        #self.Pmech_function = self.Tmax*(1-self.Wrange/self.Wmax)\ 
        #*self.Wrange 
 
        #Defines electrical power as a function of Torque 
        #self.Pelec_function =((self.Tmax*(1-self.Wrange/self.Wmax\ 
        #))/self.kt+self.Imin)*self.V 
 
        #Defines efficiency as a function of pmech and pelec 
        #self.eff_function = (self.Tmax*(1-self.Wrange/self.Wmax)*\ 
        #self.Wrange)/(((self.Tmax*(1-self.Wrange/self.Wmax))/self\ 
        #.kt+self.Imin)*self.V)  
 
        #self.Wlabel = 'Angular Velocity (rad/sec)' 
        #self.Tlabel = 'Torque (N-m)' 
        #self.Pmech_label = 'Mechanical Power (Watts)' 
        #self.Pelec_label = 'Electrical Power (Watts)' 
        #self.eff_label ='Efficiency (%)' 
 
""" def plot(self): 
        #Creates main window 
        fig = plt.figure() 
 
        #Creates four y-axis 
        ax = fig.add_subplot(111) 
        ax2 = ax.twinx() 
        ax3 = ax.twinx() 
        ax4 = ax.twinx() 
 
        #Plots the above functions against Wrange 
        ax.plot(self.Wrange,self.T_function,'-',color='black') 
        ax1.plot(self.Wrange,self.Pmech_function,'-',color='red') 
        ax2.plot(self.Wrange,self.Pelec_function,'-',color='blue') 
        ax3.plot(self.Wrange,self.eff_function,'-',color='green') 
 
        #Defines labels and grid 
        ax4.grid(True) 
        ax.set_xlabel(self.Wlabel) 
        ax.set_ylabel(self.Tlabel) 
        ax1.set_ylabel(self.Pmech_label) 
        ax2.set_ylabel(self.Pelec_label) 
        ax3.set_ylabel(self.eff_label) 
        plt.show() 
        """ 
 
test = map(1047,0.343,0.00551,0.275,6) 
print(test.Wrange) 

当我将 plot.py 作为自己的文件运行时,末尾的两条测试线按预期运行,没有错误。我该如何解决这个错误?对于构建这样的程序有什么建议吗?

编辑:我不知道 map 是标准库中的一个函数。我将来会寻找这个。我还将在以后的帖子中减少额外的代码。谢谢。

请您参考如下方法:

不要称你的类为“ map ”。那是标准库中的一个函数名,所以事情会变得困惑。您的调用 a = map(1047,0.343,x.kt,0.275,6) 正在获取标准库,这就是错误消息奇怪且令人困惑的原因。如果重命名类,事情会更有意义。

按照惯例,您的类名应以大写字母开头(部分原因是为了避免这种混淆)。