Python naming conventions guide

徘徊边缘 提交于 2021-02-08 09:42:30

问题


I would like to learn aout the naming convetions in this language, this is explained in the official documentation?, if not how I could introduce to good Python practices?


回答1:


As mentioned in the comments, you can reference the official Python style guide here. The naming conventions are here.

You can also check out Google's Python style guide here, with naming conventions addressed here.

Here are some highlights from Google:

Names to avoid

  • single character names, except for counters or iterators
  • dashes (-) in any package/module name
  • __double_leading_and_trailing_underscore__ names (reserved by Python)

Naming convention

Note that some naming conventions differ from PEP8 and instead follow the original Google Python Style guide from which this style guide originated.

  • "Internal" means internal to a module or protected or private within a class.

  • Prepending a single underscore (_) has some support for protecting module variables and functions (not included with import * from).

  • Prepending a double underscore (__) to an instance variable or method effectively serves to make the variable or method private to its class (using name mangling).

  • Place related classes and top-level functions together in a module. Unlike Java, there is no need to limit yourself to one class per module. However, make sure the classes and top-level functions in the same module have high cohesion.

  • Use CapWords for class names, but lower_with_under.py for module names. Naming examples



来源:https://stackoverflow.com/questions/27576401/python-naming-conventions-guide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!