[Tutor] Naming conventions
Mitesh H. Budhabhatti
mitesh.budhabhatti at gmail.com
Wed Apr 29 08:06:38 CEST 2015
Thank you so much Mark, Steven. This will definitely help. I really
appreciate.
On Tue, Apr 28, 2015 at 8:45 PM, Steven D'Aprano <steve at pearwood.info>
wrote:
> On Tue, Apr 28, 2015 at 01:01:04PM +0530, Mitesh H. Budhabhatti wrote:
> > Hello Friends,
> >
> > Can you please suggest good naming conventions/guidelines to following
> > while writing Python code? Thanks.
>
> Most important rule of all: names should be descriptive and
> understandable, not too short or cryptic, not so long that they are
> painful to use. Very common names may be abbreviated. Names should be
> easy to say aloud.
>
> # Good names
> length, len, map, format
>
> # Bad names
> a, the_thing_we_process, szty
>
> Avoid "funny" or meaningless names:
>
> pizza = 23
> homework = 5
> print (pizza > homework)
>
>
> It is acceptable to use standard one-letter variable names in
> generic functions, especially for algebraic (maths) functions and
> short utility functions, e.g.:
>
> i, j, k: integer loop variables
> m, n: other integers
> x, y: floats
> z: complex numbers
> s: string or set
> d: dict
> L: list (don't use l because it can look like 1)
> o: object
> u, v: vectors, arrays, sets
>
> are common choices. But don't over-use one-letter names.
>
> Otherwise, names should be self-descriptive:
>
> # Bad
> L = get_customers() # list of customers
>
> # Good
> customers = get_customers()
>
> Use plurals for lists, sets or dicts of some data type, and the singular
> for individual data types. E.g.:
>
> names = ['John', 'Mary', 'Fred', 'Sue']
> for name in names:
> process(name)
>
>
> Functions and methods should be verbs or verb-phrases in all lowercase,
> for example:
>
> expand() sort() lift() prepare_database() create_factory()
>
> Variables holding data values should be nouns:
>
> page count header footer server client factory
>
> (Watch out for words which can be both a noun and a verb, like count.)
>
> Classes should be nouns with initial capitals:
>
> Car Engine Animal HTTPServer
>
> and instances should be lowercase:
>
> server = HTTPServer()
>
> The exception is if the class is a "small" primitive or simple type,
> like built-ins int, str, dict, list etc. But even then, you may choose
> to use Initial Capitals instead.
>
> Modules should follow the all-lowercase name. If possible, modules
> should describe what they are about:
>
> math string io unicodedata
>
> but it is also acceptable for modules to have an arbitrary name which
> you just have to learn:
>
> pillow flask zope fabric nose
>
>
>
> Does this help?
>
>
> --
> Steve
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list