Home Backend Development Python Tutorial Comprehensive analysis of the scope of variables in Python starting from local variables and global variables

Comprehensive analysis of the scope of variables in Python starting from local variables and global variables

Apr 27, 2018 pm 03:31 PM
overall situation variable start

Whether it is class-based object-oriented programming or the definition of variables within a simple function, the scope of variables is always a link that must be understood and mastered in Python learning. Let's start with a comprehensive analysis of Python starting with local variables and global variables. For the scope of variables, friends who need it can refer to

Understanding global variables and local variables
1. If the variable name inside the defined function appears for the first time, and Before the = symbol, it can be considered to be defined as a local variable. In this case, regardless of whether the variable name is used in the global variable, the local variable is used in the function. For example:

  num = 100
  def func():
    num = 123
    print num
  func()
Copy after login

The output result is 123. Explain that the variable name num defined in the function is a local variable and covers the global variable. Another example:

  num = 100
  def func():
    num += 100
    print num
  func()
Copy after login

The output result is: UnboundLocalError: local variable 'num' referenced before assignment. Error message: Local variable num is applied before assignment. In other words, the variable is used incorrectly without being defined. This once again proves that what is defined here is a local variable, not a global variable.

2. If the variable name inside the function appears for the first time, appears after the = symbol, and has been defined as a global variable before, the global variable will be referenced here. For example:

  num = 100
  def func():
    x = num + 100
    print x
  func()
Copy after login

The output result is 200. If the variable name num has not been defined as a global variable before, an error message will appear: The variable is not defined. For example:

  def func():
    x = num + 100
    print x
  func()
Copy after login

The output result is: NameError: global name 'num' is not defined.

3. When a variable is used in a function, if the variable name has both global variables and local variables, the local variable will be used by default. For example:

  num = 100
  def func():
    num = 200
    x = num + 100
    prinx x
  func()
Copy after login

The output result is 300.

4. When defining a variable as a global variable in a function, you need to use the keyword global. For example:

  num = 100
  def func():
    global num
    num = 200
    print num
  func()
  print num
Copy after login

The output results are 200 and 200 respectively. This shows that the variable name num in the function is defined as a global variable and is assigned a value of 200. Another example:

  num = 100
  def func():
    global num
    num = 200
    num += 100
    print num
  func()
  print num
Copy after login

The output results are 300 and 300 respectively.

Combined with the above results of the application scenarios of global variables and local variables, I tried to do some analysis on the first half of the teaching code in the input fields (comments on the Chinese part):

  # calculator with all buttons

  import simplegui

  # intialize globals
  store = 0
  operand = 0
Copy after login

Using the simplegui module, it can be operated without error at http://www.codeskulptor.org/. However, this module cannot be used directly in the python environment, and the SimpleGUICS2Pygame package needs to be installed first.

  # event handlers for calculator with a store and operand

  def output():
  """prints contents of store and operand"""
    print "Store = ", store
    print "Operand = ", operand
    print ""
Copy after login

The global variables store and operand are used directly in the defined function output(). You can refer to point 2.

  def swap():
  """ swap contents of store and operand"""
    global store, operand
    store, operand = operand, store
    output()
Copy after login

In the defined function swap(), the global variables of store and operand are first defined. If you don't do this, an error message will appear saying that it is used without assigning a value. You can refer to point 1. At the same time, can it be understood this way: in the function swap(), when there is no keyword global, store and operand are default local variables, and it is wrong for the part on the right side of = to be used without assignment. You can refer to point 3.

  def add():
  """ add operand to store"""

    global store
    store = store + operand
    output()
Copy after login

Here I encountered the first problem since studying the two-week course: that is why the add() function only defines store as a global variable and does not define operand in the same way. Now combined with point 1, it is because store as a local variable has not been assigned a value in advance and cannot be used directly, while operand can be used by directly calling the previously defined global variable.

Variable scope
Variable scope (scope) is an easy place to fall into a trap in Python.
Python has a total of 4 scopes, which are:

L (Local) local scope
E (Enclosing) in a function outside the closure function
G (Global) global Scope
B (Built-in) Built-in scope
is searched according to the rules of L --> E --> G -->B, that is: if it is not found locally, it will be searched If you look for local parts outside the local area (such as closures), if you can't find them again, you will go to the global search, and then go to the built-in ones.

Except for def/class/lambda in Python, others such as: if/elif/else/ try/except for/while cannot change its scope. Variables defined within them can still be accessed from the outside.

>>> if True:
...   a = 'I am A'
... 
>>> a
'I am A'
Copy after login

Variable a defined in the if language is still accessible from the outside.
But it should be noted that if if is wrapped by def/class/lambda and assigned internally, it becomes the local scope of this function/class/lambda.
Assignment within def/class/lambda becomes its local scope. The local scope will cover the global scope, but will not affect the global scope.

g = 1 #全局的
def fun():
  g = 2 #局部的
  return g

print fun()
# 结果为2
print g
# 结果为1
Copy after login

But be aware that sometimes you want to reference global variables inside a function, and errors will occur if you neglect them. For example:

#file1.py
var = 1
def fun():
  print var
  var = 200
print fun()

#file2.py
var = 1
def fun():
  var = var + 1
  return var
print fun()
Copy after login

Both functions will report the error UnboundLocalError: local variable 'var ' referenced before assignment
Error in referenced before assignment! Why? Because inside the function, the interpreter detects that var has been reassigned, so var becomes a local variable, but if you want to use var before it has been assigned, this error will occur. The solution is to add globals var inside the function, but the global var will also be modified after running the function.

闭包Closure
闭包的定义:如果在一个内部函数里,对在外部函数内(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure)

函数嵌套/闭包中的作用域:

a = 1
def external():
  global a
  a = 200
  print a

  b = 100
  def internal():
    # nonlocal b
    print b
    b = 200
    return b

  internal()
  print b

print external()
Copy after login

一样会报错- 引用在赋值之前,Python3有个关键字nonlocal可以解决这个问题,但在Python2中还是不要尝试修改闭包中的变量。 关于闭包中还有一个坑:

from functools import wraps

def wrapper(log):
  def external(F):
    @wraps(F)
    def internal(**kw):
      if False:
        log = 'modified'
      print log
    return internal
  return external

@wrapper('first')
def abc():
  pass

print abc()
Copy after login

也会出现 引用在赋值之前 的错误,原因是解释器探测到了 if False 中的重新赋值,所以不会去闭包的外部函数(Enclosing)中找变量,但 if Flase 不成立没有执行,所以便会出现此错误。除非你还需要else: log='var' 或者 if True 但这样添加逻辑语句就没了意义,所以尽量不要修改闭包中的变量。

好像用常规的方法无法让闭包实现计数器的功能,因为在内部进行 count +=1 便会出现 引用在赋值之前 的错误,解决办法:(或Py3环境下的 nonlocal 关键字)

def counter(start):
    count =[start]
    def internal():
      count[0] += 1
      return count[0]
    return internal

count = counter(0)
for n in range(10):
  print count()
# 1,2,3,4,5,6,7,8,9,10

count = counter(0)
print count()
# 1
Copy after login

由于 list 具有可变性,而字符串是不可变类型。

locals() 和 globals()
globals()
global 和 globals() 是不同的,global 是关键字用来声明一个局部变量为全局变量。globals() 和 locals() 提供了基于字典的访问全局和局部变量的方式

比如:如果函数1内需要定义一个局部变量,名字另一个函数2相同,但又要在函数1内引用这个函数2。

def var():
  pass

def f2():
  var = 'Just a String'
  f1 = globals()['var']
  print var
  return type(f1)

print f2()
# Just a String
# <type &#39;function&#39;>
Copy after login

locals()
如果你使用过Python的Web框架,那么你一定经历过需要把一个视图函数内很多的局部变量传递给模板引擎,然后作用在HTML上。虽然你可以有一些更聪明的做法,还你是仍想一次传递很多变量。先不用了解这些语法是怎么来的,用做什么,只需要大致了解locals()是什么。
可以看到,locals()把局部变量都给打包一起扔去了。

@app.route(&#39;/&#39;)
def view():
  user = User.query.all()
  article = Article.query.all()
  ip = request.environ.get(&#39;HTTP_X_REAL_IP&#39;,     request.remote_addr)
  s = &#39;Just a String&#39;
  return render_template(&#39;index.html&#39;, user=user,
      article = article, ip=ip, s=s)
  #或者 return render_template(&#39;index.html&#39;, **locals())
Copy after login

The above is the detailed content of Comprehensive analysis of the scope of variables in Python starting from local variables and global variables. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A guide to using Windows 11 and 10 environment variables for profiling A guide to using Windows 11 and 10 environment variables for profiling Nov 01, 2023 pm 08:13 PM

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode for variables in PHP7: how to reduce potential bugs? Strict mode for variables in PHP7: how to reduce potential bugs? Oct 19, 2023 am 10:01 AM

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

Internal error: Unable to create temporary directory [Resolved] Internal error: Unable to create temporary directory [Resolved] Apr 17, 2023 pm 03:04 PM

Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

How to implement global loading effect in Vue How to implement global loading effect in Vue Jun 11, 2023 am 09:05 AM

In front-end development, we often have a scenario where the user needs to wait for the data to be loaded during interaction with the web page. At this time, there is usually a loading effect displayed to remind the user to wait. In the Vue framework, it is not difficult to implement a global loading effect. Let’s introduce how to implement it. Step 1: Create a Vue plug-in We can create a Vue plug-in named loading, which can be referenced in all Vue instances. In the plug-in, we need to implement the following two methods: s

PHP function introduction—is_string(): Check whether the variable is a string PHP function introduction—is_string(): Check whether the variable is a string Jul 24, 2023 pm 09:33 PM

PHP function introduction—strpos(): Check whether a variable is a string In PHP, is_string() is a very useful function, which is used to check whether a variable is a string. When we need to determine whether a variable is a string, the is_string() function can help us achieve this goal easily. Below we will learn about how to use the is_string() function and provide some related code examples. The syntax of the is_string() function is very simple. it only needs to

See all articles