Python里面所有instance应该现在__init__里面预定义

Posted panyis的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python里面所有instance应该现在__init__里面预定义相关的知识,希望对你有一定的参考价值。

原因:

Yes, you should assign all attributes of your object in the __init__ method.

The most important reason to do this is tool support. Many Python IDEs can see the assignments in the __init__ and use those attributes for type-based autocompletion. Sphinx-doc can list those attributes in your reference documentation.

来源:

两个讨论:

https://softwareengineering.stackexchange.com/questions/357937/is-it-really-correct-to-declare-all-instance-attributes-in-init?noredirect=1&lq=1#comment774986_357941

https://softwareengineering.stackexchange.com/questions/254576/is-it-a-good-practice-to-declare-instance-variables-as-none-in-a-class-in-python

例子:

Tesorflow project in Githup

image

以上是关于Python里面所有instance应该现在__init__里面预定义的主要内容,如果未能解决你的问题,请参考以下文章

python 单例模式应用

Python 单例模式

python之描述符

python class metaclass instance

如何用python get方法 获取里面的所有相同标签后的数据?

python中__init__()括号里面的变量应该如何写,分别表示啥?