简体   繁体   中英

Python naming conventions guide

我想学习一下这种语言的命名对象,这在官方文档中有解释,如果不是我如何介绍好的Python实践?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM