Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 866
y2k Avatar asked Dec 10 '22 18:12

y2k


2 Answers

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.

like image 126
Stephan202 Avatar answered Dec 22 '22 00:12

Stephan202


Read PEP 8.

It's a style guide for Python code, written by Python's creator, Guido van Rossum.

Incidentally, the answer to your question is to use underscore_case for variables and function names, and PascalCase for classes.

like image 25
Kenan Banks Avatar answered Dec 21 '22 23:12

Kenan Banks