可调用的模块
- 论坛
- 可调用的模块
7 浏览
可调用的模块
为什么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