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开发者_如何学Python are done in.
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.
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.
Seven words: Google Summer of Code Python Style Guide
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
- Packages:
lower_with_under
- Modules:
lower_with_under
,_lower_with_under
- Classes:
CapWords,
_CapWords
- Exceptions:
CapWords
- Functions:
firstLowerCapWords()
,_firstLowerCapWords()
- Global/Class Constants:
CAPS_WITH_UNDER
,_CAPS_WITH_UNDER
- Global/Class Variables:
lower_with_under
,_lower_with_under
- Instance Variables:
lower_with_under
,_lower_with_under
(protected) or__lower_with_under
(private)- Method Names:
firstLowerCapWords()
,_firstLowerCapWords()
(protected) or__firstLowerCapWords()
(private)- Function/Method Parameters:
lower_with_under
- Local Variables:
lower_with_under
精彩评论