码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-11-26 11:06:33      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:redirect   let   back   comment   com   来源   .com   ring   round   

原因:

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

技术分享图片

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

标签:redirect   let   back   comment   com   来源   .com   ring   round   

原文地址:http://www.cnblogs.com/easonbolg/p/7898330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!