检查函数调用 [英] Checking function calls

查看:70
本文介绍了检查函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个指向某个函数的变量,我可以检查某个

参数列表是否与函数在调用之前或调用时的函数匹配吗?


目前,我正在尝试在调用函数

时捕获TypeError(因为这是在尝试使用非法的

列表调用它时引发的) ,但这有一个相当不利的副作用,也就是在函数内部引发的任何TypeErrors都会捕获

。有没有办法避免这种情况?


Fredrik Tolf

If I have a variable which points to a function, can I check if certain
argument list matches what the function wants before or when calling it?

Currently, I''m trying to catch a TypeError when calling the function
(since that is what is raised when trying to call it with an illegal
list), but that has the rather undesirable side effect of also catching
any TypeErrors raised inside the function. Is there a way to avoid that?

Fredrik Tolf

推荐答案

如果列表包含,您可以匹配合法的参数数量。

func_variable.func_code.co_argcount


类型检查参数必须手动完成,因为Python是一个

动态语言。也许,您可以尝试一些类型检查装饰器。
http:// www。 ilowe.net/software/typecheck/
You can match if the list contains the legal number of arguments with.
func_variable.func_code.co_argcount

Type checking arguments has to be done manually since Python is a
dynamic language. Perhaps, you could try some typechecking decorators.
http://www.ilowe.net/software/typecheck/
从文档中,这个引发了TypeCheckError(不是TypeError),所以你
From the docs, this one raises TypeCheckError (not TypeError), so you



应该没问题。


should do fine.


Fredrik Tolf写道:
Fredrik Tolf wrote:
如果我有一个指向某个函数的变量,我可以检查是否某些
参数列表匹配函数在调用之前或调用时所需的函数?

目前,我正在尝试在调用函数时捕获TypeError
(因为这是在什么时候引发的试图用非法的
列表来调用它,但这也有相当不利的副作用,也就是在函数内部引发任何TypeErrors。有没有办法避免这种情况?

Fredrik Tolf
If I have a variable which points to a function, can I check if certain
argument list matches what the function wants before or when calling it?

Currently, I''m trying to catch a TypeError when calling the function
(since that is what is raised when trying to call it with an illegal
list), but that has the rather undesirable side effect of also catching
any TypeErrors raised inside the function. Is there a way to avoid that?

Fredrik Tolf




由于python是弱类型,你不会能够查看

" type"函数期望的参数。与您传递的argemtns类型相关的TypeErrors将仅在函数内部引发,而不是在调用它时引发的
。即当一个函数被调用时,没有类型检查,仅在它的主体中调用。


类型错误可以在调用这样的函数时引发:


func(* avar)





func(** avar)


在前一种情况下,如果avar不是序列,你将得到一个TypeError。

在后一种情况下,如果avar不是字典,你会得到一个TypeError。


在后一种情况下,如果你的字典中的键不在

参数列表中,你将得到一个TypeError。


这类似于显式指定

不在参数列表中的关键字参数(例如func(aparam = someval))。


如果你给函数提供了不正确数量的

参数,也会引发TypeError。所以,如果你想知道预期的

参数的数量,我发现这个有效:


py> def func(a,b,c):

....打印a,b,c

....

py> func.func_code.co_argcount

3


要检查参数名称,请使用


py> func.func_code.co_names

('''',''b'',''c'')

詹姆斯


-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

专栏951570

Los加利福尼亚州,加利福尼亚州90095

http://www.jamesstroud.com/


James Stroud写道:

(剪辑)
James Stroud wrote:
(snip)
因为python是弱键入,
Since python is "weakly typed",




s / weakly /动态/


(剪辑)


-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: -1] for p in p.split(''。'')])for

p in''o **** @ xiludom.gro''。split('''''') ])



s/weakly/dynamically/

(snip)

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


这篇关于检查函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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