Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming variable, best convention

What is the most used convention for naming variables in Python / Django? ex: pub_date or pubdate

What about for classes and methods?

like image 458
Nelson_Wu Avatar asked Mar 15 '11 15:03

Nelson_Wu


People also ask

What is the convention for naming a variable?

The choice of a variable name should be mnemonic — that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

What are the 3 rules for naming a variable?

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)


3 Answers

Django's coding style

like image 145
Fábio Diniz Avatar answered Oct 16 '22 13:10

Fábio Diniz


PEP 8, nothing more to say.

Of course you can use your own style (I use camelCase, for example), but most people use recommendations from that PEP.

like image 42
AndiDog Avatar answered Oct 16 '22 11:10

AndiDog


Code Complete (http://www.cc2e.com/) has some great chapters about function/variable naming, not for Python explicitly, but perhaps still interesting...

like image 2
Tobias Schittkowski Avatar answered Oct 16 '22 12:10

Tobias Schittkowski