Python运行时候现实_name_没有被定义?

Python022

Python运行时候现实_name_没有被定义?,第1张

如果你在 Python 代码中使用了 _name_ 这个变量,但运行时显示这个变量未定义,那可能是因为这个变量的值是 __main__。在 Python 中,__main__ 是一个特殊的字符串,表示这个文件中包含的代码是在主程序中执行的,而不是作为模块被导入。因此,如果你在代码中检查 _name_ 变量的值,应该将它改为 __name__。例如:

这样,当你运行这个 Python 文件时,_name_ 变量就不会未定义了。

x确实没有定义

vars()函数用来获取一个类实例的所有属性和值。

你没有定义类;

func中也没有传入这个类的对象实例。

下面是我写的例子

下面是执行后的效果

Python 3.6.2 (default, Jul 17 2017, 16:44:45) 

[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> class Student():

...     f = 123

...     def f(self):

...             return "nihoa"

... 

>>> s = Student()

>>> s.f

<bound method Student.f of <__main__.Student object at 0x109f08f28>>

>>> print(s.f)

<bound method Student.f of <__main__.Student object at 0x109f08f28>>

>>> s.f()

'nihoa'

>>>