Name conventions for variables and functions in Python?

12

In R , there is a lot of freedom and variety in function names between packages. Names with dot ( get.this ), names with camelCase ( getThis ), names with underline ( get_this ). This has its pros and cons: but the fact is that there does not seem to be a very large standardization.

I have started to study more Python and it seems to me that the concern with readability and standardization of the community is greater than in R . What are the naming conventions for functions, variables, constants etc in Python?

    
asked by anonymous 09.03.2014 / 02:54

1 answer

17

According to the Python Enhancement Proposal 8 naming conventions, some concerns include: p>

Avoid certain names

Never use the 'l', 'O', or 'I' characters as variable names because in some fonts they are indistinguishable from numbers one and zero.

Package and module names

Modules should have small names, which are written in lowercase in full.

Ex: package

Class Names

Class names have the first letter of each capitalized word (CamelCase).

Ex: ClassName

Function and method names

Function names and methods should be in lowercase letters, with words separated by underscores as useful for readability.

Ex: name_of_a_function

Note: the mixed case (all uppercase, except the first one) is only allowed in contexts where this is already the prevailing style, to maintain compatibility with previous versions.

Constants

Constants are usually defined at a module level and written in uppercase with underscores separating the words. Examples include MAX_OVERFLOW and TOTAL.

Variable names and function and method parameters

Generally, they follow the same function rule, and should be in lowercase letters with words separated by underscores as useful for readability.

Note: Use self as the first parameter of a method.

Ex: name of a method (self):

Identation

The indentation should be done using four spaces per level.

Blank lines

Blank lines are recommended to separate functions and class definitions (two lines), plus method definitions (one line).

Blanks

White space should be used to separate math, binary, comparison, and assignment operators from other elements.

Ex:

if variavel == False:
    print 2 * 3

You should avoid using white space between parentheses and parameters in the declaration of a function, as well as between a function call and the first parenthesis of your argument list.

Ex: function (first_parameter, second_parameter)

    
09.03.2014 / 03:23