Python NameError:名称未定义 [英] Python NameError: name is not defined

查看:771
本文介绍了Python NameError:名称未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,收到以下错误:

I have a python script and I am receiving the following error:

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一起运行的.

This is being run with Python 3.3.0 under Windows 7 x86-64.

为什么找不到Something类?

推荐答案

在使用该类之前,请先对其进行定义:

Define the class before you use it:

class Something:
    def out(self):
        print("it works")

s = Something()
s.out()

您需要将self作为第一个参数传递给所有实例方法.

You need to pass self as the first argument to all instance methods.

这篇关于Python NameError:名称未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆