有没有一种通用的方式让一个函数自我引用? [英] Is there a generic way for a function to reference itself?
问题描述
我可以通过下面的代码访问函数内部的python函数的属性:
def aa():
print aa .__ name__
print aa .__ hash__
#other simliar
然而, ,如果上面的 aa()
函数是写其他代码的模板,比如 bb()
,我必须写:
def bb():
print bb .__ name__
print bb .__ hash__
#other simliar
是否有类似于 self < code $> c $ c $> c $ c $> code $> def $($)
打印self .__ name__
打印自我.__ hash__
#其他simliar
I搜索并发现有人说使用该类来解决这个问题,但这可能是一个麻烦重新定义所有现有的功能。有什么建议?
函数没有通用的方法来引用自身。考虑使用装饰器。如果你想要的只是打印关于可以用装饰器轻松完成的功能的信息:
from functools import包装
def showinfo(f):
@wraps(f)
def包装(* args,** kwds):
print(f .__ name__,f .__ hash__)
返回f(* args,** kwds)
返回包装
@showinfo
def aa():
传递
如果您确实需要引用该函数,请将其添加到函数参数中:
def withself(f):
@wraps(f)
def wrapper(* args,** kwds):
return f(f,* args,** kwds)
返回包装
@withself
def aa(self):
print(self .__ name__)
#等。
编辑添加备用装饰器:
你也可以编写一个更简单(也可能更快)的装饰器,它可以使包装函数正常工作Python的内省:
def bind(f):
装饰函数f以传递引用函数
作为第一个参数
return f .__ get __(f,type(f))
@bind
def foo(self,x) :
这是一个绑定的函数!
print(self,x)
>>> foo(42)
< 0x02A46030处的函数foo> 42
>>> help(foo)
帮助模块__main__中的方法foo:
内建函数实例的foo(self,x)方法
这是一个绑定函数!
这利用了Python的描述符协议:函数有一个 __ get __
用于创建绑定方法的方法。装饰器只是使用现有的方法来使函数成为它自己的绑定方法。它只适用于独立函数,如果你想要一个方法能够引用它自己,你必须做更像原始解决方案的东西。
I can access a python function's attribute inside of function itself by below code:
def aa():
print aa.__name__
print aa.__hash__
# other simliar
However, if above aa()
function is a template for write other code, say bb()
, I have to write:
def bb():
print bb.__name__
print bb.__hash__
# other simliar
Is there a "pointer" similar to the self
argument in a class method so I could write code like this?
def whatever():
print self.__name__
print self.__hash__
# other simliar
I searched and found someone said to use the class to solve this problem, but that may be a trouble to redefine all the existing functions. Any suggestions?
There is no generic way for a function to refer to itself. Consider using a decorator instead. If all you want as you indicated was to print information about the function that can be done easily with a decorator:
from functools import wraps
def showinfo(f):
@wraps(f)
def wrapper(*args, **kwds):
print(f.__name__, f.__hash__)
return f(*args, **kwds)
return wrapper
@showinfo
def aa():
pass
If you really do need to reference the function, then just add it to the function arguments:
def withself(f):
@wraps(f)
def wrapper(*args, **kwds):
return f(f, *args, **kwds)
return wrapper
@withself
def aa(self):
print(self.__name__)
# etc.
Edit to add alternate decorator:
You can also write a simpler (and probably faster) decorator that will make the wrapped function work correctly with Python's introspection:
def bind(f):
"""Decorate function `f` to pass a reference to the function
as the first argument"""
return f.__get__(f, type(f))
@bind
def foo(self, x):
"This is a bound function!"
print(self, x)
>>> foo(42)
<function foo at 0x02A46030> 42
>>> help(foo)
Help on method foo in module __main__:
foo(self, x) method of builtins.function instance
This is a bound function!
This leverages Python's descriptor protocol: functions have a __get__
method that is used to create bound methods. The decorator simply uses the existing method to make the function a bound method of itself. It will only work for standalone functions, if you wanted a method to be able to reference itself you would have to do something more like the original solution.
这篇关于有没有一种通用的方式让一个函数自我引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!