Python code style suggestions

Source: Internet
Author: User
Python is well known for its strict Structure and Its naming rules. It is especially confusing for developers.

Especially for python and Ruby dynamic languages, rule naming is particularly important because methods or attributes may increase or decrease at any time during runtime. The syntax rules defined by the ruby language are relatively casual, but there are many implicit rules that correspond to each other at a glance. Its naming rules even penetrate into the specifications of the language itself. In terms of naming rules, Python seems to be far from standard. We need to gradually develop a good coding naming convention. This article has collected some code styles and naming rules from various major website articles. Easy to learn. Code style:
  1. Use spaces to represent indentation, rather than tab tabs. Do not mix the two.
  2. A line feed between functions must be at least one line.
  3. There must be at least two lines of lines between classes.
  4. Dict, list, tuple, and Parameter List should be added with a space before comma (,).
  5. In dict, the colon following the key should be added between value and Colon:, instead of the colon Before the key.
  6. Long Code (more than 79 chars) Use backslash \ line feed. After a new line is wrapped, the starting position of the new line should be aligned with the previous separator (that is, the line feed of the parameter is aligned with the left parenthesis (alignment
  7. Import is located after module comments and docstring, before the constant Declaration
  8. We recommend that you use Latin to encode the code file of version 3.0. UTF-8 is recommended for more than 3.0.
  9. There are operator operations in the Code. Please pay attention to the priority, the first operation is tightening, or brackets
  10. Do not add spaces on both sides of the equal sign
  11. Try not to put multiple lines of code in the same line
  12. Less use of inline comments (post-line comments)
  13. Block comments (comments of Multiline functions starting with #) to maintain the same indentation as the target function
  14. Docstring: the return value is written in the first line, and the function description is written in the next line. You can return values in the format >>> func (arg1, arg2, arg3)/R. For testing
  15. In addition, to use source code management tools such as subversion and CVS. You can write it before code after the docstring of the function.
    1. _ Version _ = "$ revision: 16dd63848921 $"
Naming rules:
  1. Use as few as possible 'l' (lowercase letter El), 'O' (uppercase letter OH), or 'I' (uppercase letter eye) as the variable name. ------- No L, I, O
  2. The package and module should contain short and all lowercase letters (modules shoshould have short, all-lowercase names.): thismoduleisnew
  3. For Class Name, we recommend that you name it in the form of capwords (uppercase): newclassname
  4. For the exception class, the string ending with an error should be named newerror.
  5. Global variables should be designed only for use in modules. When you use import *, the _ all _ mechanism in the package removes global variables.
  6. Function names should be in lowercase and words should be connected with underscores (_). In addition, mixed-size mixedcase vegetation can be used in traditional classes. Function_name (arg1 ...)
  7. Function and method arguments, self is the first parameter of the instance method, and CLS is the first parameter of the class method. If the parameter name is the same as the keyword, it should be underlined.
  8. Method Name & instance variable, the private attributes of the class instance and the private methods of the class instance start with a single underline _ private_fucntion_name (Self)
  9. Constant; all uppercase; _ lower arc interval between words: this_is_constant_var
  10. Object-oriented design suggestions:
    1. If you set the attributes of a class instance to public or private, you can set them to private first, which reduces the modification cost.
    2. Private cannot be used by a third party because it may be deleted or discarded at any time.
    3. The public attribute cannot start with an underscore.
    4. Avoid attribute operations with high computing workload
    5. If you do not want the attributes inherited by the quilt class, you should start with a double underline with no underline at last. This will enable the python naming and recognition Correction Algorithm to ensure that it is not inherited.
  1. Joined_lower can be a function name, method name, and attribute name.
  2. All_caps is a constant.
  3. Studlycaps is the class name
  4. Camelcase is only used in pre-defined naming rules
  5. Attribute interface, _ internal, _ private
  6. Avoid the _ private format as much as possible. The following two connections explain why Python does not have a private declaration.
There is no time for translation at the end. Reference: http://www.python.org/dev/peps/pep-0008/#naming-conventions programming recommendations
  • Code shoshould be written in a way that does not disadvantage other implementations of Python (pypy, Jython, ironpython, cython, psyco, and such ).

    For example, do not rely on cpython's efficient implementation of in-place String concatenation for statements in the formA + = BOrA = a + B. Those statements run more slowly in Jython. In performance sensitive parts of the library,''. Join ()Form shoshould be used instead. This will ensure that concatenation occurs in linear time when SS varous implementations.

  • Comparisons to singletons like none shoshould always be doneIsOrIs not, Never the operating ity operators.

    Also, beware of writingIf XWhen you really meanIf X is not none-- E.g. when testing whether a variable or argument that defaults to none was set to some other value. the other value might have a type (such as a container) That cocould be false in a Boolean context!

  • When implementing ordering operations with rich comparisons, it is best to implement all six operations (_ Eq __,_ Ne __,_ LT __,_ Le __,_ GT __,_ Ge __) Rather than relying on other code to only exercise a particle comparison.

    To minimize the effort involved,Functools. total_ordering ()Decorator provides a tool to generate missing comparison methods.

    Pep 207 indicates that reflexivity rulesAreAssumed by python. Thus, the interpreter may swapY> XWithX <Y,Y> = xWithX <= y, And may swap the argumentsX = yAndX! = Y.Sort ()AndMin ()Operations are guaranteed to use<Operator andMax ()Function uses>Operator. However, it is best to implement all six operations so that confusion doesn't arise in other contexts.

  • Use class-based exceptions.

    String exceptions in new code are forbidden, because this language feature is being removed in Python 2.6.

    Modules or packages shocould define their own domain-specific base exception class, which shocould be subclassed from the built-in exception class. always include a class docstring. E. g .:

    class MessageError(Exception):    """Base class for errors in the email package."""

    Class naming conventions apply here, although you shoshould Add the suffix "error" to your exception classes, if the exception is an error. Non-error exceptions need no special suffix.

  • When raising an exception, useRaise valueerror ('message ')Instead of the older formRaise valueerror, 'message'.

    The Paren-using form is preferred because when the exception arguments are long or include string formatting, you don't need to use line continuation characters thanks to the containing parentheses. the older form will be removed in Python 3.

  • When catching exceptions, mention specific exceptions whenever possible instead of using a bareExcept t:Clause.

    For example, use:

    try:    import platform_specific_moduleexcept ImportError:    platform_specific_module = None

    A bareExcept t:Clause will catch systemexit and keyboardinterrupt exceptions, making it harder to interrupt a program with control-C, and can disguise other problems. If you want to catch all exceptions that signal program errors, useFailed t exception:(Bare failed T is equivalentFailed t baseexception:).

    A good rule of thumb is to limit use of bare 'should t' clses to two cases:

    1. If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred.
    2. If the code needs to do some cleanup work, but then lets the exception propagate upwardsRaise.Try... finallyCan be a better way to handle this case.
  • Additionally, for all try/retry t clauses, limitTryClause to the absolute minimum amount of Code necessary. Again, this avoids masking bugs.

    Yes:

    try:    value = collection[key]except KeyError:    return key_not_found(key)else:    return handle_value(value)

    No:

    try:    # Too broad!    return handle_value(collection[key])except KeyError:    # Will also catch KeyError raised by handle_value()    return key_not_found(key)
  • Context managers shoshould be invoked through separate functions or methods whenever they do something other than acquire and release resources. For example:

    Yes:

    with conn.begin_transaction():    do_stuff_in_transaction(conn)

    No:

    with conn:    do_stuff_in_transaction(conn)

    The latter example doesn't provide any information to indicate that the _ enter _ and _ exit _ methods are doing something other than closing the connection after a transaction. being explicit is important in this case.

  • Use string methods instead of the string module.

    String methods are always much faster and share the same API with Unicode strings. Override this rule if backward compatibility with pythons older than 2.0 is required.

  • Use''. Startswith ()And''. Endswith ()Instead of string slicing to check for prefixes or suffixes.

    Startswith () and endswith () are cleaner and less error prone. For example:

    Yes: if foo.startswith('bar'):No:  if foo[:3] == 'bar':

    The exception is if your code must work with Python 1.5.2 (but let's hope not !).

  • Object Type comparisons shoshould always use isinstance () instead of comparing types directly.

    Yes: if isinstance(obj, int):No:  if type(obj) is type(1):

    When checking if an object is a string, keep in mind that it might be a unicode string too! In Python 2.3, STR and Unicode have a common base class, basestring, so you can do:

    if isinstance(obj, basestring):
  • For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

    Yes: if not seq:     if seq:No: if len(seq)    if not len(seq)
  • Don't write string literals that rely on significant trailing whitespace. Such trailing whitespace is always Ally indistinguishable and some editors (or more recently, reindent. py) will trim them.

  • Don't compare boolean values to true or false using=.

    Yes:   if greeting:No:    if greeting == True:Worse: if greeting is True:
  • The Python standard library will not use function annotations as that wocould result in a premature commitment to a special annotation style. Instead, the annotations are left for users to discover and experiment with useful annotation styles.

    Early core developer attempts to use function annotations revealed inconsistent, ad-hoc annotation styles. For example:

    • [STR]Was ambiguous as to whether it represented a list of strings or a value that cocould be eitherStrOrNone.
    • The notationOpen (File :( STR, bytes ))Was used for a value that cocould be eitherBytesOrStrRather than a 2-tuple containingStrValue followed byBytesValue.
    • The annotationSeek (whence: INT)Exhibited an mix of over-Specification and under-specification:IntIs too restrictive (anything_ Index __Wocould be allowed) and it is not restrictive enough (only the values 0, 1, and 2 are allowed). Likewise, the AnnotationWrite (B: bytes)Was also too restrictive (anything supporting the buffer protocol wocould be allowed ).
    • Annotations suchRead1 (N: Int = none)Were self-contradic1_sinceNoneIs notInt. Annotations suchSource_path (self, fullname: Str)-> ObjectWere confusing about what the return type shocould be.
    • In addition to the above, annotations were inconsistent in the use of concrete types versus abstract types:IntVersusIntegralAnd set/frozenset versus mutableset/set.
    • Some annotations in the abstract base classes were incorrect specifications. For example, set-to-set operations requireOtherTo be another instanceSetRather than justIterable.
    • A further issue was that annotations become part of the Specification but weren't being tested.
    • In most cases, the docstrings already defined the type specifications and did so with greater clarity than the function annotations. In the remaining cases, the docstrings were improved once the annotations were removed.
    • The observed function annotations were too ad-hoc and inconsistent to work with a coherent system of automatic type checking or argument validation. leaving these annotations in the Code wocould have made it more difficult to make changes later so that automated utilities cocould be supported.

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.