如何__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")
Run Code Online (Sandbox Code Playgroud)
这使:
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 …Run Code Online (Sandbox Code Playgroud) 使用python属性,我可以这样做
obj.y
Run Code Online (Sandbox Code Playgroud)
调用函数而不是仅返回值.
有没有办法用模块做到这一点?我有一个我想要的案例
module.y
Run Code Online (Sandbox Code Playgroud)
调用函数,而不是只返回存储在那里的值.
我是一个小包的贡献者,人们打算这样做(Foo.Bar.Bar是一个班级):
>>> from Foo.Bar import Bar
>>> s = Bar('a')
Run Code Online (Sandbox Code Playgroud)
有时候人们会误做(这Foo.Bar是一个模块):
>>> from Foo import Bar
>>> s = Bar('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)
这可能看起来很简单,但用户仍然无法调试它,我想让它更容易.我无法更改名称Foo或Bar但我想添加更多信息性的回溯,如:
TypeError("'module' object is not callable, perhaps you meant to call 'Bar.Bar()'")
我读了Callable模块 Q&A,我知道我不能__call__在模块中添加一个方法(我不想为了这个而将整个模块包装在一个类中).无论如何,我不希望模块可以调用,我只想要一个自定义的回溯.Python 3.x和2.7+有一个干净的解决方案吗?
给定一个框架对象,我需要获取相应的模块对象.换句话说,实现callers_module,这样可以:
import sys
from some_other_module import callers_module
assert sys.modules[__name__] is callers_module()
Run Code Online (Sandbox Code Playgroud)
(这将是等效的,因为我可以生成在此测试情况下,函数的堆栈跟踪.进口在那里只是为了让这个例子完整和可测试性,并防止callers_module采取使用__name__的快捷方式,因为它是在不同的模块.)
我试过这个:
import inspect
def callers_module():
return inspect.currentframe().f_back
Run Code Online (Sandbox Code Playgroud)
哪个得到一个框架对象,f_code会给我一个代码对象,但我找不到如何获取相应的模块或其名称(与sys.modules一起使用).如果我可以获取函数对象,那些函数具有__module__属性(并且还具有代码对象),但是框架中不存在这些属性.实际上,并非所有代码对象都属于函数对象,例如我的测试用例的代码(上面带有assert).对于没有模块的框架/代码对象也可以这么说 - 但是很多都是这样做的,在我的情况下它们会这样,所以不需要处理; 但是,在这种情况下,一个简单的无或例外也可以.
感觉我错过了一些简单的东西.需要做些什么才能工作?
我正在编写一个名为的模块foo,它具有许多内部函数,但最终归结为单个外部函数foo()。使用此模块/功能时,我想做
import foo
foo(whatever)
Run Code Online (Sandbox Code Playgroud)
代替
from foo import foo
foo(whatever)
Run Code Online (Sandbox Code Playgroud)
这可能吗?
python模块可以有__repr__吗?想法是做类似的事情:
import mymodule
print mymodule
Run Code Online (Sandbox Code Playgroud)
编辑:精度:我的意思是用户定义的 repr!