Second, the core style of Python Foundation

Source: Internet
Author: User
Tags class definition object model

Directory

I. Statements and syntax

Two. Variable definition and Assignment

Three. Memory management

Memory Management:

Reference count:

Simple example

Four. Python objects

Five. Identifiers

Six. Dedicated underline identifiers

Seven. Write the basic style of the module

Eight. Demonstration





I. Statements and syntax


#: Notes


\: Translate carriage return, continue on the previous line, in a long line of statements can be used to cut into multiple lines, because of its poor readability, it is not recommended to use

;: Connect two statements to one line with poor readability, not recommended

:: Separate the head and body of the code

Statements (code blocks) are indented to reflect different levels of code, with 4 spaces recommended (Do not use tab)

Python files are organized in a modular way, and writing a. py end file actually writes a module


Two. Variable definition and Assignment


A=1:1 memory variables in memory, A is a reference to a variable, Python is a dynamic language, and the variable and its type do not have to declare the type beforehand

Difference from C: a=1 no return value

Note:


The C-language variable declaration must be at the beginning of the code and be preceded by all statements

C++,java can declare variables anywhere, but must declare variable names and types

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

Variables must be assigned before they can be used

Back to Top



Three. Memory management



Memory Management:


Variable does not need to specify type

Programmers need not care about memory management

The variable will be automatically recycled

Del ability to directly free memory objects (reduce reference count of objects)

Back to Top

Reference count:


Increase reference count

object is created and its reference is assigned to a variable, reference count plus 1 (example a=1)

A reference to the same object is assigned to another variable, the reference count plus 1 (example b=a)

The object is called by the function as a parameter, and the reference count plus 1 (example int (a)

Object becomes an element in the container object, reference count plus 1 (example list_test=[' Alex ', ' Z ', a])

Reduce reference count

A as a parameter of the function call, after the function is run, all local variables including a are destroyed, the reference count minus 1

The variable is assigned to another object, and the reference count of the original object is reduced by 1 (example b=2,1 the reference to this memory object remains a)

Use Del to delete a reference to the object, minus 1 for the reference count (Example del a)

A as an element in the container list_test, is cleared and the reference count is reduced (example List_test.remove (a))

The container itself is destroyed (Example del list_test)

Note: Python memory recycling is given to a separate code, the garbage collector (with reference counters and a cyclic garbage collector), and the reference count is not cleared immediately (there may be cyclic calls) at zero


Instead of having to tangle with circular reference collection, just remember that the garbage collector helps you automatically clean up memory.

Cases:

X=1 # Creates a memory variable 1, passes a reference to the variable 1 to X, at the moment the reference count of 1 is increased to 2y=2 # of 1y=x # # to create a new memory variable 2, pass the reference to the variable 2 to Y, which points to 1 y, which is 2, so the reference count for 1 is reduced to 1del X # A reference to memory object 1 is deleted, and at the moment 1 is no longer referenced, it becomes the target of the Python interpreter's recovery.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1036857/201610/ 1036857-20161007220845957-728048402.png "alt=" 1036857-20161007220845957-728048402.png "/>

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1036857/201610/ 1036857-20161007220903004-166175292.png "alt=" 1036857-20161007220903004-166175292.png "/>

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1036857/201610/ 1036857-20161007220916035-1996753851.png "alt=" 1036857-20161007220916035-1996753851.png "/>

Four. Python objects


In Python, where the object model is used to store data, the factory function used to generate the data type is essentially a class, and the result of the new data is the instantiation of an object


There are three characteristics of the object


Identity: Memory address, can be confirmed with ID (), the same ID is the same object

Type: Can be viewed with type (), the type of the return value is also an object

Value



Five. Identifiers


Definition: A valid string collection that is allowed as a name


The name must be meaningful and readable.

The first letter must be a letter or an underscore (_)

The remaining characters can be letters and numbers or underscores

Case sensitive

Two styles: Conn_obj or Connobj

Cannot use keywords, cannot use built-in

Keyword table:



650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1036857/201610/1036857-20161007211839207-47920036. PNG "alt=" 1036857-20161007211839207-47920036.png "/>


Six. Dedicated underline identifiers


_xxx: Cannot import with from module import *

__XXX__: System-defined Name

__XXX: Private variables in class

Underscores are special for the interpreter and are symbols used by built-in identifiers, and it is not recommended that custom variables begin with an underscore


But if it's a private variable in the class, __xxx will be a good habit.


Add:


System variable __name__ will derive different values depending on how the python file is loaded

Python files are imported as modules: __name__= module name or file name

Python file executed: __name__= ' __main__ '

When we use Python to write a software, there should be only one main program that contains a lot of top-level code (that is, code without indentation).


The Python interpreter reads the top-level code immediately, and the other. py files should have only a few top-level code, all of which should be encapsulated


In a function or class


Usually at the end of the file, combine the __name__ variable and write the test code


Seven. Write the basic style of the module


1 title

2 Documentation Comments

3 Module Import

4 variable definition

5 class definition statements

6 function definition Statements

7 Main Program

8 Test Code


Eight. Demonstration


#_ *_coding:utf-8_*_

#!/usr/bin/env python


‘‘‘

This is an example module

‘‘‘


Import Sys,os


Debug=true


Class Test:

‘‘‘

Test class

‘‘‘

Pass


def main ():

‘‘‘

Test func

: return:

‘‘‘

Pass


if __name__ = = ' __main__ ':

Main ()


This article is from the "Wandering Wind" blog, please be sure to keep this source http://774148515.blog.51cto.com/10937712/1894717

Second, the core style of Python Foundation

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.