What is the naming convention in Python for variable and function names?

后端 未结 13 2315
生来不讨喜
生来不讨喜 2020-11-22 08:50

Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase:



        
13条回答
  •  攒了一身酷
    2020-11-22 09:27

    further to what @JohnTESlade has answered. Google's python style guide has some pretty neat recommendations,

    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

    • "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.
    • Use CapWords for class names, but lower_with_under.py for module names. Although there are many existing modules named CapWords.py, this is now discouraged because it's confusing when the module happens to be named after a class. ("wait -- did I write import StringIO or from StringIO import StringIO?")

    Guidelines derived from Guido's Recommendations

提交回复
热议问题