views:

72

answers:

3

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.

+6  A: 

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.

Stephan202
Noice thanks for this!
Joshua
@Stephan, Nice answer, and nice that this is just the one that took you over the 10K level!
mjv
@mjv: thanks :) It was actually another answer that helped me over the 10K, but it could have been one even before that, had the OP not accepted the answer before upvoting it. I reached my reputation cap for today at 9999 (made a screenshot :), so any points above that are due to accepted answers.
Stephan202
+4  A: 

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.

Triptych
Wow - so I answered first, but my edit/expansion makes it look like I answered second. Is that new?
Triptych
Hell ya gonan read this up my man
Joshua
+1  A: 

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
a paid nerd