python notes and naming conventions

Note

'''
这是多行注释
'''
"""
这也是多行注释
"""
print("hello,world")
#这是单行注释

Functions and methods

  • Args:
    • List the names of each parameter, and use the name after a colon and a space, separated by a description of the parameters. If the description is too long over a single line of 80 characters, use two or four spaces hanging indent (and other files portion remains the same). the description should include the type and meaning needed if a function accepts * foo (variable length parameter list) or ** bar (arbitrary keyword parameter), and should detailing * foo ** bar.
  • Returns: (or Yields: a generator)
    • Return Type Description and semantic value. If the function returns None, this part may be omitted.
  • Raises:
    • List all anomalies associated with the interface.
def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):
    """Fetches rows from a Bigtable.

    Retrieves rows pertaining to the given keys from the Table instance
    represented by big_table.  Silly things may happen if
    other_silly_variable is not None.

    Args:
        big_table: An open Bigtable Table instance.
        keys: A sequence of strings representing the key of each table row
            to fetch.
        other_silly_variable: Another optional variable, that has a much
            longer name than the other args, and which does nothing.

    Returns:
        A dict mapping keys to the corresponding table row data
        fetched. Each row is represented as a tuple of strings. For
        example:

        {'Serak': ('Rigel VII', 'Preparer'),
         'Zim': ('Irk', 'Invader'),
         'Lrrr': ('Omicron Persei 8', 'Emperor')}

        If a key from the keys argument is missing from the dictionary,
        then that row was not found in the table.

    Raises:
        IOError: An error occurred accessing the bigtable.Table object.
    """
    pass

class

class SampleClass(object):
    """Summary of class here.

    Longer class information....
    Longer class information....

    Attributes:
        likes_spam: A boolean indicating if we like SPAM or not.
        eggs: An integer count of the eggs we have laid.
    """

    def __init__(self, likes_spam=False):
        """Inits SampleClass with blah."""
        self.likes_spam = likes_spam
        self.eggs = 0

    def public_method(self):
        """Performs operation blah."""

Line comments and block comments

# We use a weighted dictionary search to find out where i is in
# the array.  We extrapolate position based on the largest num
# in the array and the array size and then do binary search to
# get the exact number.

if i & (i-1) == 0:        # True if i is 0 or a power of 2.

Naming conventions

  1. Single underscore (_) at the beginning of a block of variable or function is protected (not included when using from module import *).
  2. Double-underlined (__) method or instance variables represents the beginning of the kind of private.
Types of specification Examples
Module All lowercase, use an underscore between words my_tools
package All lowercase, use an underscore between words my_package
The class name Capitalize the first letter of each word BinaryTree
function All lowercase, use an underscore between words quick_sort()
variable All lowercase, use an underscore between words TOTAL_NUM
constant All uppercase, use an underscore between words MAX_NUM

See detailed Google open source project style guide

Guess you like

Origin www.cnblogs.com/redo19990701/p/11456754.html