【问题标题】:Return function arguments in TypeError when number of arguments are not met不满足参数数量时在 TypeError 中返回函数参数
【发布时间】:2014-05-05 16:59:07
【问题描述】:

我一直在创建自己的 python 文件,在其中创建可以在绘图时调用的函数。这样我只需要在一个文件中加载我可以用来调整我的结果的各种函数,保持我的代码简单。

我想知道的是是否可以返回一个函数缺少的参数,如果可以,如何实现?

为了说明我的意思,我定义了以下函数:

def func(integer, float):
    return integer / float

如果我现在像这样调用函数:

print func(2)

它会给出错误,指出它需要两个参数并且缺少一个。

TypeError: func() takes exactly 2 arguments (1 given)

由于多年来我将扩展我的调整文件,我会忘记一些函数和所需的参数。因此,我希望返回一个错误,说明参数的名称。

因此,在示例的情况下,我希望返回如下内容:

TypeError: func(integer, float) takes exactly 2 arguments (1 given)

我什至并不真正关心需要或给出的参数数量。我真正需要的只是func(integer, float),以防我遗漏了一个论点或给出了太多论点。

这可能吗?

提前致谢!

【问题讨论】:

  • 一旦在函数内部执行代码,我就能够返回参数名称、值和函数名称,但是当传递了错误的参数数量时,内部的代码不会被执行。那么最好的方法是查看 TypeError 异常是否可以通过调用某些方法为您提供更多信息

标签: python function arguments typeerror


【解决方案1】:

我终于设法编写了这段代码,希望对您有所帮助:

import inspect, traceback

def func(integer, float):
    return integer / float

if __name__ == "__main__":
    try:
        func(2)
    except TypeError:
        print "ERROR: 'func(a, b)' . a is an integer and b is a float"
        print traceback.format_exc()

输出:

错误:'func(a,b)'。 a 是整数,b 是浮点数 回溯(最近一次通话最后一次):
文件“c:/a/script.py”,第 9 行,在 函数(2)
TypeError: func() 只需要 2 个参数(给定 1 个)

【讨论】:

    【解决方案2】:

    我不认为你会真正想要这样做 - 但我想一种 hacky 方法是使用 inspect 模块中的 getargspec() 来确定“Python 函数参数的名称和默认值”。 , 然后将所有函数调用包装在 try/except 块中。

    >>> try:
            func(2):
        except TypeError as e:
            print "Error! The function expected {} args".format(getargspec(func).args)
            raise e
    Error! The function expected ['integer', 'float'] args 
    Traceback (most recent call last):
    File "<input>", line 5, in <module>
    TypeError: func() takes exactly 2 arguments (1 given)
    

    您也可以将其包装到自定义异常中,该异常是 TypeError 的子类(尽管在这里我们假设 TypeError 正在被引发,因为该函数未传递正确数量的参数,这可能有点过度简化)。

    请注意,您不能在函数对象本身内添加代码来执行此操作,因为 Python 在执行函数体中的任何代码之前会引发 TypeError 异常。

    >>> def another_func(arg1):
            print "Got inside the function - the arg was {}".format(arg1)
    >>> another_func("hello")
    Got inside the function - the arg was hello
    >>> another_func()
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    TypeError: another_function() takes exactly 1 argument (0 given)
    

    【讨论】:

      【解决方案3】:

      玩了一圈,找到了最让我满意的答案:

      import sys
      import numpy
      
      def func(integer = float("nan"), number = float("nan")):
          if numpy.isnan(integer):
              print "Function syntax: func(integer, number)"
              sys.exit()
          return integer * number
      
      func()
      >>> Function syntax: func(integer, number)
      
      func(1)
      >>> nan
      
      func(3,2)
      >>> 6
      

      通过将我的函数变量设置为 NaN,我只能通过在调用函数时实际给它们一个值来覆盖它们。但是,由于 if 语句,我实际上可以在调用函数时不给出任何变量来打印函数变量。

      【讨论】:

        猜你喜欢
        • 2013-12-31
        • 1970-01-01
        • 2021-08-23
        • 2012-02-14
        • 2020-05-12
        • 1970-01-01
        • 2021-07-20
        • 2013-09-20
        • 1970-01-01
        相关资源
        最近更新 更多