【问题标题】:function is not defined error in PythonPython中的函数未定义错误
【发布时间】:2011-05-13 03:05:17
【问题描述】:

我正在尝试在 python 中定义一个基本函数,但是当我运行一个简单的测试程序时,我总是得到以下错误;

>>> pyth_test(1, 2)

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    pyth_test(1, 2)
NameError: name 'pyth_test' is not defined

这是我用于此功能的代码;

def pyth_test (x1, x2):
    print x1 + x2

更新:我打开了名为 pyth.py 的脚本,然后当它给出错误时,我在解释器中输入 pyth_test(1,2)。

感谢您的帮助。 (我为这个基本问题道歉,我以前从未编程过,我正在尝试学习 Python 作为一种爱好)


import sys
sys.path.append ('/Users/clanc/Documents/Development/')
import test


printline()



## (the function printline in the test.py file
##def printline():
##   print "I am working"

【问题讨论】:

  • 如果你对函数的调用在第 1 行,那么函数定义在哪里?
  • 抛出错误的代码看起来像是在交互式会话中,但函数代码看起来像是文件的一部分。那正确吗?如果是这样,您是如何将文件导入交互式会话的?请编辑您的问题并添加更多代码以显示完整的交互式会话。 (编辑按钮位于问题底部的标签下方。)

标签: python function


【解决方案1】:

是的,但是pyth_test 的定义是在哪个文件中声明的?它是否也在它被调用之前定位?

编辑:

为了透视它,创建一个名为 test.py 的文件,其内容如下:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1,2)

现在运行以下命令:

python test.py

你应该看到你想要的输出。现在,如果你在一个交互式会话中,它应该是这样的:

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1,2)
3
>>> 

我希望这能解释声明的工作原理。


为了让您了解布局的工作原理,我们将创建一些文件。使用以下内容创建一个新的空文件夹以保持干净:

myfunction.py

def pyth_test (x1, x2):
    print x1 + x2 

program.py

#!/usr/bin/python

# Our function is pulled in here
from myfunction import pyth_test

pyth_test(1,2)

现在如果你运行:

python program.py

它会打印出 3。现在解释一下出了什么问题,让我们这样修改我们的程序:

# Python: Huh? where's pyth_test?
# You say it's down there, but I haven't gotten there yet!
pyth_test(1,2)

# Our function is pulled in here
from myfunction import pyth_test

现在让我们看看会发生什么:

$ python program.py 
Traceback (most recent call last):
  File "program.py", line 3, in <module>
    pyth_test(1,2)
NameError: name 'pyth_test' is not defined

如上所述,由于上述原因,python 无法找到该模块。因此,您应该将声明放在最前面。

现在,如果我们运行交互式 python 会话:

>>> from myfunction import pyth_test
>>> pyth_test(1,2)
3

同样的过程适用。现在,包导入并不是那么简单,所以我建议你看看modules work with Python 是如何实现的。我希望这对您的学习有所帮助并祝您好运!

【讨论】:

  • 谢谢。这很有帮助,而且确实有效。我将如何从另一个脚本或在交互式会话中调用脚本?因此,如果我关闭了 test.py 文件,我将如何运行 pyth_test 函数?谢谢!
  • 你也可以import filename然后filename.function(),所以当你阅读程序时,你可以知道函数来自哪个文件。在这种情况下import myfunction 然后myfunction.pyth_test(1,2)
【解决方案2】:

它对我有用:

>>> def pyth_test (x1, x2):
...     print x1 + x2
...
>>> pyth_test(1,2)
3

确保在调用函数之前定义函数。

【讨论】:

    【解决方案3】:

    在 python 中,函数不能从任何地方神奇地访问(就像它们在 php 中一样)。他们必须先声明。所以这会起作用:

    def pyth_test (x1, x2):
        print x1 + x2
    
    pyth_test(1, 2)
    

    但这不会:

    pyth_test(1, 2)
    
    def pyth_test (x1, x2):
        print x1 + x2
    

    【讨论】:

      【解决方案4】:

      如果您展示了用于简单测试程序的代码,将会有所帮助。直接放入解释器中,这似乎可行。

      >>> def pyth_test (x1, x2):
      ...     print x1 + x2
      ... 
      >>> pyth_test(1, 2)
      3
      >>> 
      

      【讨论】:

        【解决方案5】:

        如果使用 Python 的 IDLE 安装版本

        >>>def any(a,b):
        ...    print(a+b)
        ...
        >>>any(1,2)
        3
        

        【讨论】:

          【解决方案6】:

          检查函数名的拼写和函数调用的拼写,可能有错误。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-12-11
            • 2013-12-08
            • 2017-11-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-09
            相关资源
            最近更新 更多