Naming conventions for variables and functions in Python?

Asked

Viewed 6,546 times

15

In the R, there is a lot of freedom and variety in function names between packages. Dot names (get.this), names in camel (getThis), names in snake_case (get_this). This has its pros and cons. Why, the fact is that there doesn’t seem to be a standardization.

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

1 answer

20


According to the own nomenclature conventions Python Enhancement Proposal 8, some concerns include:

Avoid certain names

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

Package and module names

Modules should have small names, these written in lower case in full.

Ex: package

Class names

Class names have the first letter of each uppercase word (Camelcase).

Ex: Nomedeumaclasse

Function names and methods

Function and method names should be in lower case letters, with words separated by underscores as useful for readability.

Ex: function name

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

Constants

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

Variable names and function parameters and methods

They usually follow the same rule of function, and should be in lower case letters, with words separated by underscores as useful for readability.

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

Ex: method_name(self):

Identation

Identation 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 mathematical, binary, comparison and assignment operators from other elements.

Ex:

if variavel == False:
    print 2 * 3

You should avoid using blank spaces between parentheses and parameters in the declaration of a function, as well as between a function call and the first parenthesis of its argument list.

Ex: function(first parameter, second parameter)

  • William, could you put examples for all cases? I think it would look better to illustrate! Abs

  • 1

    I think it’s now more complete! Abs

  • 1

    Only one correction: PEP-8 says nothing about spaces between arguments of a function - only object spaces between parentheses (or keys) and parameters. If you look at the examples of PEP, however, you will notice that there are always spaces between the arguments (fortunately!), as in spam(ham[1], {eggs: 2}).

  • Fixed! Thanks for the reminder (=

Browser other questions tagged

You are not signed in. Login or sign up in order to post.