Search code examples
pythoncamelcasingpascalcasing

The correct case&format of variable and methods and for Python


So I know some languages have expected conventions.

PHP - underscore_case() [for the most part, lolo]

Java - camelCase()

C# - PascalCase()

etc.

What's the "Pythonic" naming convention? I know it doesn't matter in the end but just wondering if there is a "best practice" way that most modules are done in.


Solution

  • Two words: PEP 8.

    PEP 8 is the (de facto) Python style guide. Some highlights from this document (I left some stuff out on purpose; go read the original document for the ins and outs):

    • Package and Module Names: All-lowercase names. Underscores can be used in the module name if it improves readability.

    • Class Names: Almost without exception, class names use the CapWords convention.*

    • Global Variable Names: The conventions are about the same as those for functions.

    • Function Names: Function names should be lowercase, with words separated by underscores as necessary to improve readability. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

    • Method Names and Instance Variables: Lowercase with words separated by underscores as necessary to improve readability. Use one leading underscore only for non-public methods and instance variables.

    • Constants: Written in all capital letters with underscores separating words. Examples include.