Today this article is to share some of the cloud computing development tutorial, explaining that today: Python Python development actual combat operation and maintenance of automation variables.
python learning process will use a lot of data, it is easy to operate, these data are required to use a simple name stands for easy reference in the next program.
It is to represent the name of a variable data (value). Simply put variable data is to give a name
Variable naming names:
alphanumeric underscores, and can not start with a number, you can not use the keyword case-sensitive.
Naming conventions:
-
With a single variable name begins with an underscore (_X) will not be imported from module import * statement
-
Underlined before and after the variable name ( the X- ) is a system-defined variable names have special meaning to the interpreter
-
Beginning with a double underscore, but not the end of the double-underlined variable names (__X) is a local variable of class
-
Through interactive mode operation, only a single variable name underlined (_) will save the final result of the expression
python naming convention summary:
Module name: between lowercase letters, words with _ segmentation, such as ad_stats.py
Package name: the module name and the same
Class name: the word capitalized, such as AdStats ConfigUtil
Global variable names: between uppercase letters, words with _ segmentation, such as UMBER COLOR_WRITE
Ordinary variables: between lowercase letters, words with _ segmentation, such as this_is_a_var
Examples of variables: to begin with _, and other common variables, such as _price _instance_var
Private instance variables (external access being given): beginning with __ (two underscores), and other variables, like ordinary
__private_var
Proprietary variable: __ beginning, the end __, usually python own variables, not named in this way
__doc__ __class_
Variable assignment:
a variable declaration and definition of the process
Single variable assignment:
#!/usr/bin/python
-- coding: UTF-8 --
counter = 100 # integer variable assignment
miles = 1000.0 # float
name = "John" # string
print counter
print miles
print name
Multiple variable assignment:
Python allows you to assign multiple variables simultaneously.
E.g:
a = b = c = 1
Examples of the above, an object to create an integer, a value of 1, three variables are assigned to the same memory space.
At the same time assign different values to multiple variables.
E.g:
a, b, c = 1, 2, “john”
The above example, two integer objects 1 and 2 is allocated to the variable a and B, string objects "john" to the variable c.
Way to store data variables:
general programming language variables to store your data:
Is a variable region of the computer's memory, you can store the variable value within a predetermined range, and the value is variable.
It will open up a space in memory to create variables. Based on the data type of the variable, the interpreter allocates specified memory, and decide what data can be stored in memory. Thus, the variable can specify different data types, these variables may be stored integer, decimal or character.
For example, c language After declaring a variable a, it will open up in the memory space corresponding together, in this space can store different values, that is, assign different values to variables
python variable is stored in memory and other programming languages is worth a different way:
In Python, there is no type of variable names, but there are objects
Variable name just a reference to the object (internal implementation pointer)
python is the main data, only a variable corresponding to a tag memory space, a space to store an open 1 = 1, a = 2 after the re-copying to re-open a new storage space 2, a change of a variable name point to the new location in space 2
The same address space may have two or more labels, such as a = 1, b = 1 and b is actually a point to the same address space
View variable points to the address space Address: Using id (variable name) function
>>> a=1
>>> id(a)
19882304
>>> b=1
>>> id(b)
19882304
Assign the same values were found for the different variables, the real address space is not changed, but change tag
PYTHON inside the reference count (SYS.GETREFCOUNT):
What is the reference counter:
Python using internal records of all the objects in the number of references. An internal tracking variables, called a reference counter. When an object is created, it creates a reference count, when the object is no longer needed, that is to say, the object's reference count goes to zero, it is garbage collected. (This is only the image of the talk about, is not strictly 100% correct, but the popular understanding is often the best way to learn)
Increase the reference count:
When an object is created and (to quote) assigned to a variable, the object is referenced technology is set to 1. When the same object of the application or the object has been assigned to other variables, or passed to a function, method, or class instance as a parameter or is assigned a window object members when a new reference to the object, or called alias, is created (the reference count of the object is automatically incremented)
Reducing the reference count:
When a reference to an object is destroyed, the reference count will be reduced. The most obvious example is when references out of its scope, this most often occurs at the end of the function runs, all local variables are automatically destroyed, the object's reference count will be reduced.
When the variable is assigned to another object, the source object reference technique also automatically decremented
Other causes object's reference count reduction methods include the use of del statement removes a variable, or when an object's reference count will be reduced in the following circumstances:
-
A local reference left its scope, such as the end of the function
-
Destruction alias object is displayed
-
An alias object is assigned to another object
-
The object is removed from a window object
-
Window object itself is destroyed
example:
import sys
a=“ab”
sys.getrefcount ( "ab")
3 results for the first 3
b=“ab”
sys.getrefcount ( "ab")
Results of the second 4 +1
b = 0 b refer to other objects (0), for "ab" was canceled in terms of a reference
sys.getrefcount ( "ab")
3 results on the basis of the last referenced -1
Note: Objects with spaces in the interactive interpreter is always citations 3, but return to normal in the script, for example:! # / Usr / bin / env python # coding = utf8 fdaf import sys print sys.getrefcount ( "ab cd ") a =" ab cd "print sys.getrefcount (" ab cd ") b =" ab cd "print sys.getrefcount (" ab cd ") c = b print sys.getrefcount (" ab cd ")
Garbage collection:
No longer used memory garbage collection mechanism is called release. As stated above, although the interpreter tracked object's reference count, but the garbage collector is responsible for freeing memory. The garbage collector is a separate code, which is used to find an object reference count of 0, although he is also responsible for checking that the reference count is greater than zero but also the objects being destroyed. Certain circumstances lead to a circular reference.
A circular reference occurs when you have at least two objects refer to each other, also known as the references are gone, these references still exist, indicating the reference count alone is not enough. Python's garbage collector is actually a reference to a cycle counter and garbage collector. When the total amount of an object's reference count goes to 0, the interpreter will pause, freed this object and other objects of this object is only accessible as a supplement to the reference count, the garbage collector will be carefully allocated large ( as well as those) of the object is not destroyed by reference counting. In this case, the interpreter will pause, trying to clean up all references to cycle.