python - 什么时候在 Python 中初始化一个类变量?
问题描述
考虑以下 Python 3 代码:
class A:
b = LongRunningFunctionWithSideEffects()
什么时候会LongRunningFunctionWithSideEffects()
调用?目前模块已导入?或者目前该类首次以某种方式使用?
解决方案
目前模块已导入
test.py
:
def x():
print('x')
class A:
x = x()
然后
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
x
推荐阅读
- ssis - 如何在 SSIS 派生列的列中删除带有数字的字符串?
- java - 方法合并不能应用于给定类型
- javascript - Firebase:获取当前聊天室(或任何抽象资源)中的用户数量
- wamp - Wamp:收到“配置失败”错误
- r - 在数据框中重新编码多个数值变量
- javascript - 如何用找到的不区分大小写的正则表达式替换匹配项,但扩展结果
- powershell - Powershell 查找和替换字符串
- excel - VBA 循环:如何将 Ticker 符号放入 Ticker 列?
- python - Python无法检查值是否在列表中
- jenkins-pipeline - docker-workflow-plugin (docker-pipeline-plugin) java.io.IOException: 无法 rm 容器