模块上的 __getattr__
- 2025-01-10 08:47:00
- admin 原创
- 99
问题描述:
__getattr__
如何在类、模块上实现等效功能?
例子
当调用模块的静态定义属性中不存在的函数时,我希望在该模块中创建一个类的实例,并使用与模块上的属性查找失败相同的名称调用该方法。
class A(object):
def salutation(self, accusative):
print "hello", accusative
# note this function is intentionally on the module, and not the class above
def __getattr__(mod, name):
return getattr(A(), name)
if __name__ == "__main__":
# i hope here to have my __getattr__ function above invoked, since
# salutation does not exist in the current namespace
salutation("world")
得出:
matt@stanley:~/Desktop$ python getattrmod.py
Traceback (most recent call last):
File "getattrmod.py", line 9, in <module>
salutation("world")
NameError: name 'salutation' is not defined
解决方案 1:
您在这里遇到了两个基本问题:
__xxx__
方法只能在类中查找TypeError: can't set attributes of built-in/extension type 'module'
(1)意味着任何解决方案都必须跟踪正在检查的模块,否则每个模块都会有实例替换行为;(2)意味着(1)甚至不可能......至少不是直接的。
幸运的是,sys.modules 对内容并不挑剔,因此包装器可以工作,但仅适用于模块访问(即import somemodule; somemodule.salutation('world')
;对于同一模块访问,您几乎必须从替换类中提取方法并将它们添加到globals()
类上的自定义方法(我喜欢使用.export()
)或通用函数(例如已经列出的答案)。有一件事要记住:如果包装器每次都创建一个新实例,而全局解决方案不是,那么您最终会得到略有不同的行为。哦,你不能同时使用两者 - 只能选择其中之一。
更新
来自Guido van Rossum:
实际上,有一种偶尔使用并推荐的技巧:模块可以定义一个具有所需功能的类,然后在最后用该类的实例(或者,如果您坚持的话,用该类替换 sys.modules 中的自身,但这通常不太有用)。例如:
# module foo.py
import sys
class Foo:
def funct1(self, <args>): <code>
def funct2(self, <args>): <code>
sys.modules[__name__] = Foo()
这是可行的,因为导入机制正在积极启用此 hack,并在加载后将其实际模块从 sys.modules 中拉出。(这并非偶然。此 hack 很久以前就被提出,我们决定在导入机制中支持它。)
因此,实现您想要的目标的既定方法是在模块中创建一个单独的类,并将其作为模块的最后一项操作替换为sys.modules[__name__]
类的一个实例 - 现在您可以根据需要使用__getattr__
// 。__setattr__
`__getattribute__`
注 1:如果您使用此功能,则模块中的任何其他内容(例如全局变量、其他函数等)将在进行sys.modules
分配时丢失 - 因此请确保所需的一切都在替换类中。
注2:为了支持from module import *
你必须__all__
在类中定义;例如:
class Foo:
def funct1(self, <args>): <code>
def funct2(self, <args>): <code>
__all__ = list(set(vars().keys()) - {'__module__', '__qualname__'})
根据您的 Python 版本,可能还有其他名称需要从 中省略__all__
。set()
如果不需要 Python 2 兼容性,则可以省略。
解决方案 2:
不久前,Guido 宣布新式类中的所有特殊方法查找都会绕过__getattr__
和__getattribute__
。Dunder 方法之前可以在模块上使用 - 例如,在这些技巧失效__enter__
之前,您只需定义和即可将模块用作上下文管理器。__exit__
最近,一些历史特性又重新回归,__getattr__
其中包括模块,因此现有的 hack(模块在sys.modules
导入时用类替换自身)已经不再必要。
在 Python 3.7+ 中,你只需使用一种显而易见的方法。要自定义模块上的属性访问,请__getattr__
在模块级别定义一个函数,该函数应接受一个参数(属性名称),并返回计算值或引发AttributeError
:
# my_module.py
def __getattr__(name: str) -> Any:
...
这还将允许挂钩到“来自”导入,即,您可以返回诸如这样的语句的动态生成的对象from my_module import whatever
。
相关提示:除了模块 getattr 之外,您还可以__dir__
在模块级别定义一个函数来响应dir(my_module)
。有关详细信息,请参阅PEP 562。
解决方案 3:
这是一个 hack,但是你可以用类包装模块:
class Wrapper(object):
def __init__(self, wrapped):
self.wrapped = wrapped
def __getattr__(self, name):
# Perform custom logic here
try:
return getattr(self.wrapped, name)
except AttributeError:
return 'default' # Some sensible default
sys.modules[__name__] = Wrapper(sys.modules[__name__])
解决方案 4:
我们通常不这么做。
我们做的就是这个。
class A(object):
....
# The implicit global instance
a= A()
def salutation( *arg, **kw ):
a.salutation( *arg, **kw )
为什么?这样隐式的全局实例才是可见的。
例如,查看random
模块,它创建一个隐式全局实例,以稍微简化您想要“简单”随机数生成器的用例。
解决方案 5:
与@Håvard S 提出的类似,如果我需要在模块上实现一些魔法(比如__getattr__
),我会定义一个继承自的新类types.ModuleType
并将其放入sys.modules
(可能替换定义我的自定义的模块ModuleType
)。
请参阅Werkzeug__init__.py
的主文件来了解相当强大的实现。
解决方案 6:
这很黑客,但是...
# Python 2.7
import types
class A(object):
def salutation(self, accusative):
print("hello", accusative)
def farewell(self, greeting, accusative):
print(greeting, accusative)
def AddGlobalAttribute(classname, methodname):
print("Adding " + classname + "." + methodname + "()")
def genericFunction(*args):
return globals()[classname]().__getattribute__(methodname)(*args)
globals()[methodname] = genericFunction
# set up the global namespace
x = 0 # X and Y are here to add them implicitly to globals, so
y = 0 # globals does not change as we iterate over it.
toAdd = []
def isCallableMethod(classname, methodname):
someclass = globals()[classname]()
something = someclass.__getattribute__(methodname)
return callable(something)
for x in globals():
print("Looking at", x)
if isinstance(globals()[x], (types.ClassType, type)):
print("Found Class:", x)
for y in dir(globals()[x]):
if y.find("__") == -1: # hack to ignore default methods
if isCallableMethod(x,y):
if y not in globals(): # don't override existing global names
toAdd.append((x,y))
# Returns:
# ('Looking at', 'A')
# ('Found Class:', 'A')
# ('Looking at', 'toAdd')
# ('Looking at', '__builtins__')
# ('Looking at', 'AddGlobalAttribute')
# ('Looking at', 'register')
# ('Looking at', '__package__')
# ('Looking at', 'salutation')
# ('Looking at', 'farewell')
# ('Looking at', 'types')
# ('Looking at', 'x')
# ('Looking at', 'y')
# ('Looking at', '__name__')
# ('Looking at', 'isCallableMethod')
# ('Looking at', '__doc__')
# ('Looking at', 'codecs')
for x in toAdd:
AddGlobalAttribute(*x)
if __name__ == "__main__":
salutation("world")
farewell("goodbye", "world")
# Returns:
# hello world
# goodbye world
它通过遍历全局命名空间中的所有对象来实现。如果项目是一个类,它会遍历类属性。如果属性是可调用的,它会将其作为函数添加到全局命名空间。
它忽略所有包含“__”的属性。
我不会在生产代码中使用它,但它应该可以帮助你入门。
解决方案 7:
这是我自己的一些微薄贡献 - 对@Håvard S 的高度评价的答案进行了稍微的修饰,但更加明确(因此它可能被@S.Lott 接受,尽管对于 OP 来说可能不够好):
import sys
class A(object):
def salutation(self, accusative):
print "hello", accusative
class Wrapper(object):
def __init__(self, wrapped):
self.wrapped = wrapped
def __getattr__(self, name):
try:
return getattr(self.wrapped, name)
except AttributeError:
return getattr(A(), name)
_globals = sys.modules[__name__] = Wrapper(sys.modules[__name__])
if __name__ == "__main__":
_globals.salutation("world")
解决方案 8:
对于模块,请参阅PEP-562,自 3.7 版开始存在:
(注:写完这篇文章后,我意识到Wim 的回答就是这么说的,但当然,我不知怎么地错过了翻阅所有现已过时的答案的机会 :-(
尽管从收听精彩的 Python Bytes 播客中得知有此功能,但仍然如此。
我们确实,确实需要在答案上加一个弃用标志。
尽管如此,我还是希望有人会发现实际的工作代码很有用。
我无法理解 PEP,因此进行了一些测试来使其发挥作用:
模块1.py
from datetime import date, datetime
_OBSOLETES = {
"oldanswer" : 24
}
datenow = date.today()
def __getattr__(name):
if name == "timenow":
return datetime.now()
if (v:=_OBSOLETES.get(name)):