use*_*514 31 python reference function self
我可以通过下面的代码访问函数内部的python函数的属性:
def aa():
print aa.__name__
print aa.__hash__
# other simliar
Run Code Online (Sandbox Code Playgroud)
但是,如果上面的aa()函数是用于编写其他代码的模板,比方说bb(),我必须写:
def bb():
print bb.__name__
print bb.__hash__
# other simliar
Run Code Online (Sandbox Code Playgroud)
是否有类似于self类方法中的参数的"指针",所以我可以编写这样的代码?
def whatever():
print self.__name__
print self.__hash__
# other simliar
Run Code Online (Sandbox Code Playgroud)
我搜索并发现有人说使用该类来解决这个问题,但重新定义所有现有功能可能会有麻烦.有什么建议?
Dun*_*can 28
函数没有通用的方式来引用它自己.考虑使用装饰器.如果你想要的只是打印有关可以使用装饰器轻松完成的功能的信息:
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
Run Code Online (Sandbox Code Playgroud)
如果您确实需要引用该函数,那么只需将其添加到函数参数中:
def withself(f):
@wraps(f)
def wrapper(*args, **kwds):
return f(f, *args, **kwds)
return wrapper
@withself
def aa(self):
print(self.__name__)
# etc.
Run Code Online (Sandbox Code Playgroud)
编辑以添加备用装饰器:
您还可以编写一个更简单(也可能更快)的装饰器,它将使包装函数在Python内省中正常工作:
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!
Run Code Online (Sandbox Code Playgroud)
这利用了Python的描述符协议:函数有一个__get__用于创建绑定方法的方法.装饰器只是使用现有方法使函数成为自身的绑定方法.它只适用于独立函数,如果您希望某个方法能够引用自身,则必须执行更像原始解决方案的操作.
aki*_*ira 14
http://docs.python.org/library/inspect.html看起来很有希望:
import inspect
def foo():
felf = globals()[inspect.getframeinfo(inspect.currentframe()).function]
print felf.__name__, felf.__doc__
Run Code Online (Sandbox Code Playgroud)
您还可以使用该sys模块获取当前函数的名称:
import sys
def bar():
felf = globals()[sys._getframe().f_code.co_name]
print felf.__name__, felf.__doc__
Run Code Online (Sandbox Code Playgroud)