如何从函数中获取函数名称? [英] how to get name of function from within function?

查看:105
本文介绍了如何从函数中获取函数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从函数中获取函数的名称。东西

喜欢:


def myFunc():

print __myname __

myFunc()
''myFunc''


我真正要做的就是轻松去除前缀一个函数

名称并调用另一个函数。像这样:


def prefix_myFunc(a,b,c):

name = __myname __ [7:]

call(名字,a,b,c)


这会调用myFunc(a,b,c)。


如何实现这一目标' '优雅'',错误...'''pythonicly''.. =)


另外,有没有办法将普通的位置args变成一个元组而没有

使用*?像这样:


def f(a,b,c):

print get_args_as_tuple()f(1,2,3)



(1,2,3)


我不想用

def f(* args):

print args

因为我想保留使用

固定数量的错误检查功能位置args。


感谢您的帮助。

解决方案

Christopher J. Bottaro写道:

我想从函数中获取函数的名称。
如:

def myFunc():
print __myname__

myFunc()' 'myFunc''


有一个非常好的方法可以做到这一点。你能否提供一些更详细的信息

你究竟想要做什么?取决于函数'

该函数中的名称可能是一个坏主意...


你*可以*使用sys._getframe执行此操作()hack:


py> def my_func():

.... print sys._getframe()。f_code.co_name

....

py> my_func()

my_func


但这取决于非公共API。另外,你想发生什么?如果有人为你的功能绑定了另一个名字,你会发生什么?b $ b上面的代码将

打印原始名称,即使它与新名称不同:


py> g = my_func

py> del my_func

py> g()

my_func

py> my_func()

回溯(最近一次调用最后一次):

文件"<互动输入>",第1行,在?

NameError:名称''my_func''未定义

此外,有没有办法将正常位置args转换为元组而不使用*?像这样:

def f(a,b,c):
print get_args_as_tuple()
f(1,2,3)



(1,2,3)




嗯...


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

....打印(a,b,c)

....

PY> f(1,2,3)

(1,2,3)





STeVe


Steven Bethard写道:

Christopher J. Bottaro写道:

我想得到这个名字函数内的函数。


def myFunc():
print __myname__

> myFunc()


''myFunc''



没有一个非常好的方法来做到这一点。你能否提供一些更详细的信息
你想在这里做什么?




我想围绕功能做包装,但我可以''使用__getattr__因为

函数作为SOAP服务发布而SOAP lib我是
使用需要一个可调用对象作为发布的参数

函数。


基本上我想在try / except中自动包装每个函数。

通常情况下,我想人们会这样做__getattr__。我的

代码现在的方式是每个函数(错误,方法)都包含在确切的

相同的try / except错误处理代码中。每次我创建一个新函数时,我都会b / b
复制/粘贴try / catch块。它的丑陋,它使代码变得混乱,它容易出错,如果我决定稍后更改错误处理代码,我就有了b $ b来改变大量的功能。实际上,我刚刚意识到函数调用参数没有传递给

__getattr__,所以我想用这种方式调用函数调用就不行了。


在Python中有类似PHP的__call()方法吗?

此外,是否有一种方法将正常位置args转换为元组而不使用*?像这样:

def f(a,b,c):
print get_args_as_tuple()

> f(1,2,3 )



(1,2,3)



嗯...

py> def f(a,b,c):
... print(a,b,c)
...
py> f(1,2,3)
(1,2,3)




在你的方法中,如果名称和/或位置参数的数量发生变化,

函数的主体必须改变。我想避免这种情况。


谢谢。


Christopher J. Bottaro写道:
< blockquote class =post_quotes>基本上我想在try / except中自动包装每个函数。




最简单的方法就是:


def wrapFunction(func):

def包装器(* args,** kargs):

试试:

return func(* args,** kargs)

除了Exception,e:

raise#你的错误处理代码

return wrapper


HTH


I want to get the name of the function from within the function. Something
like:

def myFunc():
print __myname__

myFunc() ''myFunc''

Really what I want to do is to easily strip off the prefix of a function
name and call another function. Like this:

def prefix_myFunc(a, b, c):
name = __myname__[7:]
call(name, a, b, c)

That would call myFunc(a, b, c).

How do I accomplish this ''elegantly'', err...''pythonicly''..=)

Also, is there a way to turn normal positional args into a tuple without
using *? Like this:

def f(a, b, c):
print get_args_as_tuple() f(1, 2, 3)


(1, 2, 3)

I don''t want to use
def f(*args):
print args
because I want to keep the error checking capability that comes with using a
fixed number of positional args.

Thank you for the help.

解决方案

Christopher J. Bottaro wrote:

I want to get the name of the function from within the function. Something
like:

def myFunc():
print __myname__

myFunc() ''myFunc''
There''s not a really good way to do this. Can you give some more detail
on what exactly you''re trying to do here? Depending on a function''s
name within that function is probably a bad idea...

You *can* do this using a sys._getframe() hack:

py> def my_func():
.... print sys._getframe().f_code.co_name
....
py> my_func()
my_func

But that depends on a non-public API. Also, what do you want to happen
if someone binds another name to your function? The code above will
print the original name even if it''s different from the new name(s):

py> g = my_func
py> del my_func
py> g()
my_func
py> my_func()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name ''my_func'' is not defined
Also, is there a way to turn normal positional args into a tuple without
using *? Like this:

def f(a, b, c):
print get_args_as_tuple()
f(1, 2, 3)



(1, 2, 3)



Um...

py> def f(a, b, c):
.... print (a, b, c)
....
py> f(1, 2, 3)
(1, 2, 3)

?

STeVe


Steven Bethard wrote:

Christopher J. Bottaro wrote:

I want to get the name of the function from within the function.
Something like:

def myFunc():
print __myname__

> myFunc()


''myFunc''



There''s not a really good way to do this. Can you give some more detail
on what exactly you''re trying to do here?



I want to make wrappers around functions but I can''t use __getattr__ because
the functions are being published as a SOAP service and the SOAP lib I''m
using requires a callable object as the argument to the publishing
function.

Basically I want to wrap every function in try/except automatically.
Typically, I suppose people would do that with __getattr__. The way my
code is now is that every function (err, method) is enclosed in the exact
same try/except error handling code. Everytime I make a new function, I
copy/paste that try/catch block. Its ugly, it clutters the code, its prone
to error, and if i decide to change the error handling code later, I have
to change tons of functions.

Actually, I just realized that function call arguments are not passed to
__getattr__, so my idea of wrapping function calls that way won''t work.

Is there something like PHP''s __call() method in Python?

Also, is there a way to turn normal positional args into a tuple without
using *? Like this:

def f(a, b, c):
print get_args_as_tuple()

>f(1, 2, 3)



(1, 2, 3)



Um...

py> def f(a, b, c):
... print (a, b, c)
...
py> f(1, 2, 3)
(1, 2, 3)

?



In your method, if the name and/or number of positional arguments changes,
the body of the function must change. I want to avoid that.

Thanks.


Christopher J. Bottaro wrote:

Basically I want to wrap every function in try/except automatically.



Simplest way to do that would be something like:

def wrapFunction(func):
def wrapper(*args, **kargs):
try:
return func(*args, **kargs)
except Exception, e:
raise # your error handling code here
return wrapper

HTH


这篇关于如何从函数中获取函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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