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

posted @ 2017-11-26 09:56  Eason的博客  阅读(525)  评论(0编辑  收藏  举报