我有一个python脚本,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\Tim\Desktop\pop-erp\test.py", line 1, in <module>
s = Something()
NameError: name 'Something' is not defined
以下是导致问题的代码:
s = Something()
s.out()
class Something:
def out():
print("it works")
这是在Windows 7 x86-64下使用Python 3.3.0运行的。
为什么不能找到Something
课程?
在使用之前定义类:
class Something:
def out(self):
print("it works")
s = Something()
s.out()
您需要将self
作为所有实例方法的第一个参数传递。
您必须在创建类的实例之前定义类。将Something
的调用移动到脚本的末尾。
您可以尝试将购物车放在马前并在定义之前调用程序,但这将是一个丑陋的黑客,您将必须按照此处的定义滚动您自己: