在一个 .py 文件中,我有以下内容:
class avgbox:
def __init__(self,t,n,a):
self.t = t
self.n = n
self.a = a
def add(x):
self.t += x
self.n += 1
self.a = t/n
现在,当我在从Ubuntu命令行运行的交互式Python shell中执行
from file.py import *
时,它没有显示任何错误。然后,当我执行a = avgbox(0,0,0)
时,它显示NameError: name 'avgbox' is not defined
。有什么想法吗?我做错了什么?谢谢!
from foo import *
几乎总是一个坏主意。 - Woobleself
作为add
方法的第一个参数! - Peter Varofrom file.py import *
应该会报错。请尝试使用from file import *
。 - Ankur Ankanavgbox
。请尝试使用from file import avgbox
,如果这不起作用,请重新启动您的交互式解释器。 - Sven Marnach