Python Naming conventions

Source: Internet
Author: User
Tags instance method

1. Module Name:

  Usually lowercase letters, between words separated by _:

   ad_stats.py

2. Package Name:

Same as the module name

3. Class Name:

The class name uses the specification of the beginning of the word capitalization (Pascal naming style). Example:adstats, Configutil

    • The class name that begins with _ single underscore is used internally , and the From M import * is not imported by default.
4. Variables: 1) global variable names (class variables, equivalent to static variables in Java):
    • uppercase letters, dividing between words with _ Number , Color_write
    • For from M Import * Import statements, if you want to prevent global variables within the import module from using the old specification, add a leading underscore to the global variable.
    • Note: You should avoid using global variables
2) Common variables:
    • lowercase letters, dividing between words with _ This_is_a_var
    • Attention:
      • Neither the class member variable nor the global variable uses the M or G prefixes.
      • private class members are identified by using a single underscore prefix , multiple definitions expose members, and less defined private members.
      • Variable names should not have type information, because Python is a dynamic type language. such as Ivalue, Names_list, dict_obj , etc. are not a good name.
3) Instance variables:
    • start with _ , other as _price as normal variables , _instance_var
4) Private instance variable (external access will error):
    • Starting with __ (2 underscores), the other is the same as the normal variable __private_var the proprietary variable:
    • Note: __ begins, __ ends, is usually python's own variable, do not name __doc__, __class__ in this way
5. Function 1) normal function:

As with normal variables: get_name () count_number () Ad_stat ()


2) Private function (external access will error):
    • Start with __ (2 underscores), others as normal functions __get_name ()
3) file name:
    • Full lowercase, underline available
4) parameters of functions and methods:
    • Always use "self" as the first parameter of an instance method. Always use "CLS" as the first parameter of a class method.
    • If a function's parameter name conflicts with reserved keywords, it is usually better to use a suffix underline than to use abbreviations or strange spellings.
5) Constants:
    • The constant name is all letters capitalized, and each word is connected by an underscore, such as max_overflow,total.
6) exception
    • Because exceptions are also a class, follow the naming conventions for classes.
    • In addition, if the exception actually refers to an error, you should use "error" as the suffix.
6. Abbreviations

The name should use all spelling words as far as possible , the abbreviation is as follows two kinds of things:
1. Commonly used abbreviations, such as XML, ID, and so on, should be named with only the first letter , such as Xmlparser.

2. The name contains long words, and a word is abbreviated. You should use the abbreviated method of the contract idiomatic.

For example, the function abbreviation is fn,text abbreviated to txt,object abbreviated to Obj,count abbreviated to cnt,number abbreviated to NUM, and so on.

7. Summary of leading suffix underscores
    • A leading underscore (_XXX): Represents a non-public: Represents a class attribute that cannot be accessed directly by a class object and a subclass object through the provided interface and cannot be imported with ' from module import * ' (equivalent to private)
    • __xxx__ System Definition Name
    • A suffix underscore (__xxx): is a private member, meaning that only the class object can access it by itself, and even the subclass object cannot access the data.
    • Two leading underscores: used when naming a class property that causes a name conflict.
    • Two leading and suffix underscores (__xxx__): System reserved word nomenclature, such as __init__ or __file__. Never create names like this, just use them .

Python Naming conventions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.