Good programmers share Python automated operations development lessons four – variables

Introduction: 1. What is a variable 2. The name of the variable 3. Reference counting What is a variable:

There is a lot of data to use in Python. To make it easier to use, separate the data into a simple name that you can refer to in subsequent programs.
A variable is a name that represents some data (value). A variable is simply a name for the data

Variable name naming:

The value consists of alphanumeric underscores (_) and is case sensitive. It cannot start with a digit. Keywords cannot be used.
Naming conventions:

1. Variable names (_X) starting with a single underscore will not be imported by from Module import * statements. Underlined variable names (_X_) are system-defined variable names that have special meaning for the interpreter. Variable names (__X) that begin with a double underscore but do not end with a double underscore are local variables to the class 4. When run in interactive mode, the variable name (_) with a single underscore holds the result of the final expression

Summary of python naming conventions:

Module name: lowercase letters, separated by _ between words, such as ad_stats.py Package name: same as module name Class name: uppercase letters, such as AdStats ConfigUtil Global variable name: uppercase letters, separated by _ between words, such as UMBER COLOR_WRITE Common variable: Case letters, words separated by _, such as this_is_a_var instance variables: starts with _, other variables are the same as normal variables, such as _price _instance_var private instance variables (external access will give an error) : __private_var private variables: start with __ and end with __. They are usually Python’s own variables. Do not name __doc__ __class_ assignments this way:

Is the process of variable declaration and definition
Single variable assignment:

#! /usr/bin/python# -*- coding: Utf-8 -*-counter = 100 # Assign integer variable Miles = 1000.0 # float name = “John” # string print counterprint Milesprint name

Multiple variable assignments:

Python allows you to assign values to multiple variables at the same time. For example, if a = b = c = 1, create an integer object with the value 1 and allocate the three variables to the same memory space. Assign different values to multiple variables simultaneously. For example: a, b, c = 1, 2, “John” in the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object “John” is assigned to variable C. How variables store data:

Common programming language variables store data in the following ways:

A variable is an area of computer memory that can store a specified range of values, and the values are variable. A space is created in memory when a variable is created. Based on the variable’s data type, the interpreter allocates specified memory and decides what data can be stored in memory. Therefore, variables can specify different data types, and these variables can store integers, decimals, or characters. For example, after a variable a is declared in C, a corresponding space will be opened up in memory, in which different values can be stored, that is, different values can be assigned to variables

Python variables are stored in memory differently than in other programming languages:

In Python, a variable name has no type, but an object has a variable name that is just a reference to the object (internally implemented as a pointer). In Python, the variable a is just a label for a memory space. A =1 creates a space to store 1 and then copies it again. The same address space can have two or more labels, such as a=1,b=1. In fact, a and B refer to the same address space. Use id(variable name) function >>> A =1 >>> ID (a) 19882304 >>> B =1 >>> ID (b) 19882304 to find the same value assigned to different variables, the actual address space does not change, The reference count inside PYTHON (sys.getrefcount) has changed:

What is a reference counter?


Python internally keeps track of how many references there are to all objects in use. An internal trace variable, called a reference counter. When an object is created, a reference count is created, and when the object is no longer needed, that is, when the object’s reference count becomes zero, it is garbage collected. (This is just to say the image, not strictly 100% correct, but popular understanding is often the best way to learn)
Increment reference count:
When an object is created and assigned to a variable, the object’s reference technique is set to 1. When an application to the same object is either assigned to another variable, passed as an argument to a function, method, or class instance, or assigned as a member of a window object, a new reference, or alias, to the object is created (and the object’s reference count is automatically incresed by one).
Reducing reference count:

When a reference to an object is destroyed, the reference count is reduced. The most obvious example is when a reference leaves its scope. This happens most often at the end of a function run, when all local variables are automatically destroyed and the object’s reference count is reduced.

When a variable is assigned to another object, the reference technique for the source object is also automatically reduced by one
Other ways that an object’s reference count can be reduced include using the DEL statement to remove a variable, or an object’s reference count can be reduced if:

1. A local reference leaves its scope, such as the end of a function 2. The alias of the object is displayed destroyed 3. An alias for an object is assigned to another object. 4. The object is removed from a window object. The window object itself is destroyed

Example:

> > > import sys > > > a = “ab” > > > sys. Getrefcount (” ab “) 3 for the first time the results for 3 > > > b = “ab” > > > sys. Getrefcount (” ab “) 4 + 1 second results > > > b = 0 >>> sys. getrefCount (“ab”)3 Results in -1 based on the last reference

Note: Whitespace object references are always 3 in the interactive interpreter, 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:

Memory that is no longer used is freed by a mechanism called garbage collection. As mentioned above, although the interpreter keeps track of the object’s reference count, the garbage collector is responsible for freeing memory. The garbage collector is a separate piece of code that looks for objects with a reference count of zero. It also checks for objects with a reference count greater than zero that should also be destroyed. Certain situations lead to circular references. A circular reference occurs when you have at least two objects that refer to each other, that is, the references disappear, and those references still exist, which shows that reference counting alone is not enough. Python’s garbage collector is really a reference counter and a circular garbage collector. When an object’s reference count goes to zero, the interpreter pauses, freeing that object and any other objects accessible only to that object. To supplement the reference count, the garbage collector also keeps an eye out for objects that have a large total allocation (and those that are not destroyed by reference counting). In this case, the interpreter pauses in an attempt to clean up any loops that are references.