The second day of Python-core style of the Foundation, and core of python on the second day

Source: Internet
Author: User

The second day of Python-core style of the Foundation, and core of python on the second day

I. syntax and statements

#: Comment

\: Press enter to continue the previous line. You can use it to split a line into multiple lines when the statement is long. It is not recommended because of its poor readability.

;: Connects two statements to one line, which has poor readability and is not recommended.

: Separate the header and body of the Code

The statement (code block) uses indentation to display different code levels. Four spaces are recommended (do not use tab)

Python files are organized as modules. Writing a file ending with. py actually writes a module.

 

Ii. variable definition and assignment

A = 1; 1: The Memory variables are stored in the memory, a is a reference to the variables, python is a dynamic language, and the variables and their types do not need to be declared in advance.

The difference between a and c is that a = 1 has no return value.

PS:

The C language variable Declaration must be at the beginning of the code and before all statements

C ++ and java can declare variables at any time, but the variable names and types must be declared.

Python can declare variables anytime, anywhere. However, when a variable is defined, the interpreter determines its type based on the value on the right of the equation.

The variable must be assigned a value first.

 

Iii. Memory Management

You do not need to specify the type of the variable.

Programmers do not need relationship Memory Management

The variable is automatically recycled.

Del can directly release memory formation (reduce the reference count of objects)

What is the reference count?

Add reference count:

1. The object is created and assigned a value to the variable. The reference count is + 1 (for example, a = 1)

2. the reference of the same object is assigned to other variables. Reference count + 1 (B =)

3. The object is called by the function as a parameter. Reference count + 1 (int ())

  4. The object becomes an element in the container object. Reference count + 1 (list = ['chars _ lee ', 'Man', a])

Reduce reference count:

1. As a parameter called by the function, after the function is executed, all local parameters, including a, will be destroyed. Reference count-1

2. The variable is assigned to another object. The reference count of the original object is-1 (for example, a = 1, B = a, B = 2)

3. Use del to delete object references. Reference count-1 (del)

4. When a is cleared as a container element, reference count-1 (for example, list. remove ())

5. the container itself is destroyed, reference count-1 (del list)

Note: The python memory is recycled to an independent code, that is, the spam Recycle Bin (including reference counters and cyclic garbage collectors). The reference count is not cleared immediately when it is set to zero (it may be called cyclically)

You don't have to tangle with loop reference collection, just remember that the garbage collector will automatically clean up your memory.

Simple Example

 

X = 1 # Create memory variable 1 and pass the reference of variable 1 x, at this moment, the reference count of 1 is 1y = x #1. The reference count is increased to 2y = 2 # create a new memory variable of 2 and pass the reference of variable 2 to y, originally pointing to y of 1, 2 is given at the moment, so reference count of 1-1del x # deletes reference x of Memory Object 1. At this moment, 1 no reference is provided, at this moment, it becomes the target of python interpreter collection.

 

In this case, when a = 1, the memory will open up a space. When B = 1, the memory should open up a new space, however, use the id function to check whether the memory addresses of a and B are the same. Why?

Because the number 1 occupies a small amount of memory. Frequent memory application is a waste. Therefore, python provides an optimization mechanism. This type of length, or memory usage is small. Python will cache

 

Iv. python objects

In Python, object models are used to store data. Factory functions used to generate data types are essentially classes. The result of creating a new data is actually instantiating an object.

Objects have three major features:

Identity: memory address. You can use id () to confirm that the same object has the same id.

Type: You can view it with type (). The type returned value is also an object.

Value

 

5. identifier

Definition: a set of valid strings allowed as names

The name must be meaningful and readable.
The first letter must be a letter or underscore (_)
The remaining characters can be letters, numbers, or underscores.
Case Sensitive
Two styles: conn_obj or ConnObj
You cannot use keywords or built-in keywords.

Keyword table:

Built-in: automatically imported by the interpreter (which provides basic functions) and can be viewed as a global variable,

 

6. Special underline identifier

_ Xxx: import from module import * is unavailable.

_ Xxx __: system-defined name

_ Xxx: private variable in the class

Underlines are special for interpreters and are symbols used by built-in identifiers. We do not recommend that you start with a line below a custom variable.

But if it is a private variable in the class, __xxx will be a good habit

Supplement:

  • When we use python to write a software program, there should be only one main program that contains a large number of top-level code (that is, code without indentation,

    The python interpreter reads the top-level code and executes it immediately). Other. py files should have only a small amount of top-level code, and all functions should be encapsulated.

    In a function or class

  • Usually combine the _ name _ variable at the end of the file to write the test code.
  • 7: Compile the basic python Style

    Title

    Document comment

    Module Import

    Variable definition

    Class Definition Statement

    Function Definition Statement

    Main Program

    Test code

    For example

    # _ * _ Coding: UTF-8 _*_#! /Usr/bin/env python ''' This is an example module # The three quotation marks are multi-line comments. The functions of This py file are usually described in This section, '''import sys, OS # import system module debug = True # define the variable (the top write indicates the complete set is valid) class Test: # define the class '''test class''' passdef main (): # define the function '''test func: return: '''passif _ name _ = '_ main _': main ()

     

    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.