变量作用域
Posted chriszun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了变量作用域相关的知识,希望对你有一定的参考价值。
python中的作用域分4种情况:
L:local,局部作用域,即函数中定义的变量;
E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部作用域,但不是全局的;
G:globa,全局变量,就是模块级别定义的变量;
B:built-in,系统固定模块里面的变量,比如int, bytearray等。
(1)变量查找顺序:LEGB,作用域局部>外层作用域>当前模块中的全局>python内置作用域;
(2)只有模块、类、及函数才能引入新作用域;
(3)对于一个变量,内部作用域先声明就会覆盖外部变量,不声明直接使用,就会使用外部作用域的变量;
(4)内部作用域要修改外部作用域变量的值时,全局变量要使用global关键字,嵌套作用域变量要使用nonlocal关键字。nonlocal是python3新增的关键字,有了这个关键字,就能完美的实现闭包了。
res = None
def calc(a,b):
res = a+b
calc(1,2)
print(res) # 结果 None
res = None
def calc(a,b):
res = 0
res = a+b
print(res)
calc(1,2)
print(res) #结果:None
res = None
def calc(a,b):
global res
res = a+b
calc(1,2)
print(res)# 3
res2 = None
def calc(a,b):
global res
res = a+b
calc(1,2)
print(res) #3
res = None
def calc(a,b):
res = a+b
global res
calc(1,2)
print(res) # 报错,def变更作用域,res为局部变量,但没有赋值就直接使用,报 res is not defined
res = None
def calc(a,b):
global res
res = 0
res = a+b
calc(1,2)
print(res) # 结果:3
res = [1]
def calc():
res[0] = 2
print(res)
calc()
print(res) # 报错,name ‘res‘ is assigned to before global declaration,内部作用域要修改外部作用域变量的值时,全局变量要使用global关键字
res = 1
def calc(a,b):
res = 0
res = a+b
# global res
calc(1,2)
print(res) # 报错,res is not defined
money = 0
def tom():
global money
money = 100
def jack():
global money
money = money - 50
tom()
jack()
print(‘jack消费后剩余%s‘ % money) # 50
================================================
def tom():
global money
money = 100
def jack():
global money
money = money - 50
tom()
jack()
print(‘jack消费后剩余%s‘ % money) # 50
========================================
d = {}
def test():
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/p/10530261.html‘
def test2():
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/‘
test()
test2()
print(d) #{}
================================================
def test():
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/p/10530261.html‘
def test2():
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/‘
test()
test2()
print(d) #报错,name d is not defined
===========================================
def test():
global d
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/p/10530261.html‘
def test2():
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/‘
test()
test2()
print(d) #{‘url‘:‘https://www.cnblogs.com/uncleyong/p/10530261.html‘}
===============================================
def test():
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/p/10530261.html‘
def test2():
global d
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/‘
test()
test2()
print(d) #{‘url‘:‘https://www.cnblogs.com/uncleyong/‘}
======================================
def test():
global d
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/p/10530261.html‘
def test2():
global d
d = {}
d[‘url‘]=‘https://www.cnblogs.com/uncleyong/‘
test()
test2()
print(d) #{‘url‘:‘https://www.cnblogs.com/uncleyong/‘}
================================
info ={‘age‘:18, ‘url‘:‘https://www.cnblogs.com/uncleyong/p/10530261.html‘}
def test():
global info
info={}
info[‘name‘]=‘qzcsbj‘
test()
print(info) #{‘name‘:qzcsbj‘}
=======================================
info ={‘age‘:18, ‘url‘:‘https://www.cnblogs.com/uncleyong/p/10530261.html‘}
def test():
info={}
info[‘name‘] = ‘qzcsbj‘
test()
print(info) #{‘age‘:18, ‘url‘:‘https://www.cnblogs.com/uncleyong/p/10530261.html‘}
============================================
info ={‘age‘:18, ‘url‘:‘https://www.cnblogs.com/uncleyong/p/10530261.html‘}
def test():
info[‘age‘]=info[‘age‘]+1
info[‘name‘] = ‘qzcsbj‘
test()
print(info) #{‘age‘:19, ‘url‘:‘https://www.cnblogs.com/uncleyong/p/10530261.html‘,‘name‘: ‘qzcsbj‘}
==========================
info =1
def test():
info=2
print(info)
test()
print(info) #2 1
========================
list1 =[1,2,3]
def test():
list1[0]=2
print(list1)
test()
print(list1) #[2,2,3][2,2,3]
=====================================
s = ‘https://www.cnblogs.com/uncleyong/p/10530261.html‘
def test():
global s
s = ‘test‘
test()
print(s) # test
===================================
s = [1, 2, 3]
def test():
s[0] = 123
test()
print(s) # [123,2,3]
=========================
s = [1, 2, 3]
def test():
s = []
s.append(123)
test()
print(s)# [1,2,3]
============================
s = [1, 2, 3]
def test():
global s
s[0] = 123
test()
print(s) #[123,2,3]
===============================
s = [1, 2, 3]
def test():
global s
s = []
s.append(123)
test()
print(s)# [123]
===========================
s = (1,2,3)
def test():
s[0]=123
test()
print(s) # x is not undefined
==========================
s = (1,2,3)
def test():
global s
s[0]=123
test()
print(s) #元组不可修改,‘\\‘tuple\\‘ object does not support item assignment‘
s = (1,2,3)
def test():
s = (4,5)
test()
print(s) #(1,2,3)
=======================
s = (1,2,3)
def test():
global s
s = (4,5)
test()
print(s) #(4,5)
=========================
s = {1,2,3}
def test():
s.add(5)
test()
print(s) # {1,2,3,5}
========================
s = {1,2,3}
def test():
global s
s.add(5)
test()
print(s) # {1,2,3,5}
============================
s = {1,2,3}
def test():
s = set()
s.add(5)
test()
print(s) #{1,2,3}
=====================
s = {1,2,3}
def test():
global s
s = set()
s.add(5)
test()
print(s) #{5}
‘‘‘
def outer():
count = 10
def inner():
nonlocal count
count = 20
print(count)
inner()
print(count)
outer()
结果:20 20
以上是关于变量作用域的主要内容,如果未能解决你的问题,请参考以下文章