Alibabacloud.com offers a wide variety of articles about python global variable in class, easily find your python global variable in class information here online.
http://www.crifan.com/summary_python_variable_effective_scope/Explaining the scope of variables in PythonExample:1. Code version1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 """4 -------------------------------------------------------------------------------5 Function:6 "Grooming" python: the Meaning of self and init__ + why you have self and __init__7 Http://www.crifan.com/summary_the_meaning_of_self_and___init___in_python_and_why_need_them8 9 Author:cr
In Python, a local variable is defined as a local variable, and the scope of the local variable is local. If the local variable is defined inside the function body, if you want to invoke the local variable outside the body of the
You should try to avoid using global variables. Different modules are free to access global variables, which can lead to unpredictable global variables. For global variables, if programmer a modifies the value of _a, programmer B also uses _a, which can cause errors in the program. Such errors are difficult to detect a
Avoid using global variables whenever possible. Different modules can freely access global variables, which may lead to unpredictable global variables. For global variables, if programmer A modifies the value of _ A, programmer B also uses _ A, which may cause errors in the program. It is difficult to find and correct
1 GlobalThe global statement and its nonlocal cousin is the only things that is remotely like declaration statements in Python. They is not type or size declarations; They is namespace declarations. The global statement tells Python, a function plans to change one, or more global
When we create a module of our own, the module itself comes with several global variables, which are present in each file.View py Global variables for the current file[[email protected] ~]# echo ' Print (VARs ()) ' > scripts.py [[email protected] ~]# python scripts.py {' __builtins__ ': Default global variables
Python refers to the order of variables: the current scope local variable-> outer scope variable-> the global variable in the current module->python built-in variable
One,
This article mainly introduces how to implement cross-file global variables in python. For more information about how to use Python to compile applications, sometimes the same global variable is transferred between multiple files. This article provides the following solution
1. What is a local variableA local variable is a variable that has a local effect and is defined inside a function. The scope is only within the function, the variables outside the function is not affected, that is, the same name function, if not in the function to do the global DeclarationThen there's no effect on the outside of the function.1x ="Alex"2 3 deffun
Python global variables do not conform to the relevant rules of parameter passing, so many people do not want to use them frequently unless constants are defined. We will discuss global variables and find the hidden tips. I hope you will gain some gains through this article.
The program is roughly like this:
CONSTANT=0
defmodifyConstant():
printCONST
Analysis of global usage instances in python
This example describes global usage in python. Share it with you for your reference. The specific analysis is as follows:
1. global --- define a variable as a
that the function can access global variables.
Therefore, the problem is that, because the variable CONSTANT is modified inside the function, Python considers CONSTANT as a local variable, and print CONSTANT is before CONSTANT + = 1, this error will certainly occur.
So, how should we access and modify
Look at the code first:Code One:#!/usr/bin/python#coding:utf8x = def test ():p rint ' x= ', xtest ()Code One execution Result:x = 20We have a slight modification on the basis of code one.Code two:x = def test ():p rint ' x= ', xx = 2 print ' Change X to ', Xtest ()Code Two execution results:X=traceback (most recent): file "D:\Demo\testGlobal.py", line to Parsing the problem: we tried to reassign X to 2 in line 4th, and because of the scope of the
num = 5def func ():num + = 1Print (num)Print (num)Output:unboundlocalerror:local variable ' num ' referenced before assignmentTranslation: Local variable error: the variable "num" is not defined and is applied. This proves once again that a local variable is defined, not a global
In python, global variables are generally used in two ways:
First, define it in a separate module, and then import the defined global variable module in the global module to be used.
Type 2: Define it directly in the current module, then directly declare it through
The examples in this article describe how Python implements global variables through function properties. Share to everyone for your reference. The specific analysis is as follows:
Python functions can define attributes, and they are global, which is very useful, for example, for numeric accumulation, you don't have
This example describes the global usage in Python. Share to everyone for your reference. The specific analysis is as follows:
1, Global---defines a variable as a global variable. You can change the value of a
This article mainly introduces the global usage in python, analyzes the global function in detail, and demonstrates the corresponding usage skills, for more information about global usage in python, see the following example. Share it with you for your reference. The specifi
-in namespaces: These are built-in namespaces, such as list, tuple, and STR, which are stored in the Python interpreter for us to provide names.2. Global namespaces: In the py file, the variables of the outer-function life belong to the global namespace.3. Local namespaces: The variables of life in the function are placed in the local namespace.Scope:1.
Global vs. local variables1. Variables defined in subroutines are called local variables, and variables defined at the beginning of a program are called global variables.2, the scope of global variables is the entire program, the scope of the local variable is the subroutine that defines the
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.