In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What this article shares with you is about how to analyze the variables in the development of Python automation operation and maintenance. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Good programmers share Python automation operation and maintenance development practice four-variable
Introduction: 1. What is variable 2. Naming of variable names 3. Variable assignment 4. The way variables store data 5. Reference count
What is a variable:
Python learning process will use a lot of data, then in order to facilitate the operation, the data need to be represented by a simple name, easy to refer to in the following program.
A variable is the name that represents a data (value). To put it simply, a variable is a name for the data.
Naming of variable names:
Consisting of alphanumeric underscores, cannot start with a number, cannot use keywords, and is case-sensitive.
Naming convention:
1. Variable names that begin with a single underscore (_ X) are not imported into 2. 0 by the from module import * statement. The underlined variable name (_ X _) is the system-defined variable name, which is of special significance to the interpreter. The variable name (_ _ X) that begins with a double underscore but ends without a double underscore is the local variable of the class 4. When running in interactive mode, only a single underlined variable name (_) saves the result of the final expression
Summary of python naming conventions:
Module name: lowercase letters. Words are separated by _, such as ad_stats.py package name: same class name as module name: first letter uppercase, such as AdStats ConfigUtil global variable name: uppercase, and words are separated by _, such as UMBER COLOR_WRITE ordinary variables: lowercase letters, words are separated by _, such as this_is_a_var instance variables: start with _, other variables are the same as ordinary variables For example, _ price _ instance_var private instance variable (external access will report an error): start with _ _ (2 underscores), other private_var-specific variables are the same as ordinary variables: _ _ start, end, and generally are python's own variables. Do not name _ _ doc__ class_ in this way.
Variable assignment:
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 # floating point 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: a = b = c = 1 or more, create an integer object with a value of 1, and three variables are assigned to the same memory space. Assign different values to multiple variables at the same time. For example: a, b, c = 1,2, "john" above, two integer objects 1 and 2 are assigned to variables an and b, and the string object "john" is assigned to variable c.
How variables store data:
The way general programming language variables store data:
A variable is an area of computer memory that can store values within a specified range, and the values are variable. A space is opened up in memory when a variable is created. Based on the data type of the variable, the interpreter allocates the specified memory and determines 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 declaring a variable a, the c language will open up a corresponding space in memory, in which different values can be stored, that is, different values can be assigned to the variable.
Python variables are stored in memory differently than in other programming languages:
In Python, the variable name has no type, but the variable name of the object is only a reference to the object (internal implementation is a pointer). Python is dominated by data, variable an is just a label equivalent to a memory space, axiom 1 opens up a piece of space storage 1, and then re-replicates a new space storage 2, variable name a changes location to point to 2 in the new space. The same address space can have two or more tags. For example, axiom 1 is actually an and b pointing to the same address space to view the address of the variable pointing to the address space: using id (variable name) function > > axi1 > id (a) 19882304 > > bread1 > id (b) 19882304 the previous example found that the same value is assigned to different variables, the actual address space has not changed, but the label has changed.
Reference count within PYTHON (SYS.GETREFCOUNT):
What is a reference counter:
Python internally keeps track of how many references there are to all objects in use. An internal tracking 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 0, it is garbage collected. (this is just a visual statement, it is not strictly 100% correct, but popular understanding is often the best way to learn.)
Increase the reference count:
When an object is created and assigned to a variable, the reference technique for that object is set to 1. When an application of the same object or an object is assigned to another variable, or when it is passed as a parameter to a function, method, or class instance, or when it is assigned as a member of a window object, a new reference to that object, or an alias, is created (the reference count of the object is automatically incremented by 1)
Reduce the reference count:
When references to an object are destroyed, the reference count is reduced. The most obvious example is that when a reference leaves its scope, this most often occurs at the end of a function run, where all local variables are automatically destroyed and the reference count of the object is reduced.
When a variable is assigned to another object, the reference technique of the source object is also automatically subtracted by 1.
Other ways to reduce an object's reference count include deleting a variable using a de statement, or when an object's reference count decreases in the following cases:
1. A local reference is out of its scope, such as the end of the function 2. The alias of the object is displayed to destroy 3. An alias of the object is assigned to the other object 4. Object is removed from a window object. The window object itself is destroyed
Example:
> > import sys > a = "ab" > sys.getrefcount ("ab") 3 the first result is 3 > > b = "ab" > sys.getrefcount ("ab") 4 the second result + 1 > > bicon0b refers to other objects (0). For "ab", it cancels a reference > sys.getrefcount ("ab") 3 on the basis of the last citation.
Note: the number of references to objects with spaces in the interactive interpreter is always 3, but returns 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") croomb 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 tracks the reference count of objects, 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 0. It is also responsible for checking objects that should be destroyed even though the reference count is greater than 0. Certain situations can lead to circular references. A circular reference occurs when you have at least two objects referencing each other, that is, when the so-called references disappear, these references still exist, indicating that reference counting is not enough. Python's garbage collector is actually a reference counter and a circular garbage collector. When the reference count of an object changes to 0, the interpreter pauses and releases the object and other objects that are accessible only by that object, as a supplement to the reference count. The garbage collector also pays attention to objects that are allocated a large total (and those that are not destroyed by reference counting). In this case, the interpreter pauses to try to clean up all loops that are references.
The above is how to analyze the variables in the development of Python automation operation and maintenance. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.