我有一个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作为第一个参数传递给所有实例方法.

Python-3.x相关问答推荐

将f-字符串放置在f-字符串内

为什么我的Selenium脚本在密码元素上失败?

为什么我在BLE中的广告代码在发送包裹之间需要大约1秒

向前/向后移动导致移动行的数据不可见

对大型数据框中的选定列进行重新排序

基于Pandas列动态创建分箱,以使观测值数量或计数占总计数的1%.

Python base64.b32hexencode 未创建预期结果

如何在 histplot 中标记核密度估计

Jupyter Notebook 拒绝打印一些字符串

Python ** 用于负数

用于 BIG 数组计算的多处理池映射比预期的要慢

Dask worker post-processing

Pytorch:图像标签

python3源的类图查看器应用程序

创建一个可旋转的 3D 地球

Python 错误:IndexError:字符串索引超出范围

sys.stdin.readline() 读取时没有提示,返回 'nothing in between'

如何使用 d.items() 更改 for 循环中的所有字典键?

Python 3.5:async with导致 SyntaxError.为什么?

如何删除目录? os.removedirs 和 os.rmdir 是否只用于删除空目录?