可调用的模块

7 浏览
0 Comments

可调用的模块

为什么Python不允许模块拥有__call__方法?(除了显而易见的直接导入会变得困难之外。)具体来说,为什么使用a(b)的语法不能像对函数、类和对象那样找到__call__属性?(模块的查找方式是否存在不兼容的差异?)

>>> print(open("mod_call.py").read())
def __call__():
    return 42
>>> import mod_call
>>> mod_call()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'module' object is not callable
>>> mod_call.__call__()
42

0