【转】局部变量和全局变量---------------【答不对,你还敢说你精通、熟悉python?】

转载:https://www.cnblogs.com/uncleyong/p/11230413.html    局部变量和全局变量

 1 #== == == == == == == == 第一部分 == == == == == == == == ==
 2 res = None
 3 def calc(a, b):
 4     res = a + b
 5 calc(1, 2)
 6 print(res)
 7 #上面代码结果是:None
 8 
 9 res = None
10 def calc(a, b):
11     res = 0
12     res = a + b
13 calc(1, 2)
14 print(res)
15 #上面代码结果是:None
16 
17 res = None
18 def calc(a, b):
19     global res
20     res = a + b
21 calc(1, 2)
22 print(res)
23 #上面代码结果是:3
24 
25 res2 = None
26 def calc(a, b):
27     global res
28     res = a + b
29 calc(1, 2)
30 print(res)
31 #上面代码结果是:3 
32 
33 # res = None
34 # def calc(a, b):
35 #     res = a + b
36 #     global res
37 # calc(1, 2)
38 # print(res)
39 #上面代码结果是:运行报错    name 'res' is assigned to before global declaration
40 
41 res = None
42 def calc(a, b):
43     global res
44     res = 0
45     res = a + b
46 calc(1, 2)
47 print(res)
48 #上面代码结果是:3
49 
50 # res = None
51 # def calc(a, b):
52 #     res = 0
53 #     global res
54 #     res = a + b
55 # calc(1, 2)
56 # print(res)
57 #上面代码结果是:运行报错
58 
59 # res = None
60 # def calc(a, b):
61 #     res = 0
62 #     res = a + b
63 #     global res
64 # calc(1, 2)
65 # print(res)
66 #上面代码结果是:运行报错
money = 0
def tom():
global money
money = 180
print(id(money))
def jack():
global money1
money1=150
money = money1 - 50
print(id(money))
tom()
jack()
print('jack消费后剩余%s' % money)
#上面代码结果是: 100

def tom():
global money
money = 100
def jack():
global money
money = money - 50
tom()
jack()
print('jack消费后剩余%s' % money)
#上面代码结果是:50 (不是-50) 问题1、这里重复定义全局变量money后,它还保留之前的值100 可以这样理解吗 ?
 

 

 

 

  1 # == == == == == == == == 第二部分 == == == == == == == == ==
  2 d = {}
  3 def test():
  4     d['url'] = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
  5 def test2():
  6     d['url'] = 'https://www.cnblogs.com/uncleyong/'
  7 test()
  8 test2()
  9 print(d)
 10 # 上面代码结果是: {'url': 'https://www.cnblogs.com/uncleyong/'}
 11 
 12 def test():
 13     d = {}
 14     d['url'] = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
 15 def test2():
 16     d = {}
 17     d['url'] = 'https://www.cnblogs.com/uncleyong/'
 18 test()
 19 test2()
 20 print(d)
 21 # 上面代码结果是: {'url': 'https://www.cnblogs.com/uncleyong/'}(不会报错)
 22 
 23 def test():
 24     d = {}
 25     d['url'] = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
 26 def test2():
 27     global d
 28     d = {}
 29     d['url'] = 'https://www.cnblogs.com/uncleyong/'
 30 test()
 31 test2()
 32 print(d)
 33 # 上面代码结果是:{'url': 'https://www.cnblogs.com/uncleyong/'}
 34 
 35 info = {'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'}
 36 def test():
 37     global info
 38     info = {}
 39     info['name'] = 'qzcsbj'
 40 test()
 41 print(info)
 42 # 上面代码结果是:{'name': 'qzcsbj'}
 43 
 44 info = {'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'}
 45 def test():
 46     info = {}
 47     info['name'] = 'qzcsbj'
 48 test()
 49 print(info)
 50 # 上面代码结果是:{'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 = {}
info['name'] = 'qzcsbj'
print(info) #打印结果 {'name': 'qzcsbj'},使用当前局部变量的值,不是全局变量的值
test()
print(info)
# 上面代码结果是:{'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'}

===============
 51 
 52 info = {'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'}
 53 def test():
 54     info['age'] = info['age'] + 1
 55 test()
 56 print(info)
 57 # 上面代码结果是:{'age': 19, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'}
 58 
 59 s = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
 60 def test():
 61     s = 'test'
 62 test()
 63 print(s)
 64 # 上面代码结果是:https://www.cnblogs.com/uncleyong/p/10530261.html
 65 
 66 url = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
 67 def test():
 68     s = 'test'
 69 test()
 70 print(s)
 71 # 上面代码结果是:https://www.cnblogs.com/uncleyong/p/10530261.html (没报错,取的第63行的s)
 72 
 73 url = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
 74 def test():
 75     global s
 76     s = 'test'
 77 test()
 78 print(s)
 79 # 上面代码结果是:test
 80 
 81 s = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
 82 def test():
 83     global s
 84     s = 'test'
 85 test()
 86 print(s)
 87 # 上面代码结果是:test
 88 
 89 s = [1, 2, 3]
 90 def test():
 91     s[0] = 123
 92 test()
 93 print(s)
 94 # 上面代码结果是:[123, 2, 3]  数组也是全局变量?
 95 
 96 s = [1, 2, 3]
 97 def test():
 98     s = []
 99     s.append(123)
100 test()
101 print(s)
102 # 上面代码结果是: [1, 2, 3]
103 
104 s = [1, 2, 3]
105 def test():
106     global s
107     s[0] = 123
108 test()
109 print(s)
110 # 上面代码结果是:[123, 2, 3]  也算是全局变量再被定义时,仍保留了原来的值?
111 
112 s = [1, 2, 3]
113 def test():
114     global s
115     s = []
116     s.append(123)
117 test()
118 print(s)
119 # 上面代码结果是:[123]  把原来的值清空了
120 
121 # s = (1, 2, 3)
122 # def test():
123 #     s[0] = 123
124 # test()
125 # print(s)
126 # 上面代码结果是:这句 s[0] = 123 报错  'tuple' object does not support item assignment  元组不能被修改
127 
128 # s = (1, 2, 3)
129 # def test():
130 #     global s
131 #     s[0] = 123
131 #   print(s[0]) #打印结果是 1
132 # test() 133 # print(s) 134 # 上面代码结果是:这句 s[0] = 123 报错 'tuple' object does not support item assignment 元组不能被修改 135 136 s = {1, 2, 3} 137 def test(): 138 s.add(5) 139 test() 140 print(s) 141 # 上面代码结果是:{1, 2, 3, 5} 142 143 s = {1, 2, 3} 144 def test(): 145 global s 146 s.add(5) 147 test() 148 print(s) 149 # 上面代码结果是:{1, 2, 3, 5} 150 151 s = {1, 2, 3} 152 def test(): 153 s = set() #这里是定义了一个局部变量,而不是将s={1,2,3}转换为元组 154 s.add(5) #这里是局部变量 155 test() 156 print(s) 157 # 上面代码结果是:{1, 2, 3} (需要注意) 158 159 s = {1, 2, 3} 160 def test(): 161 global s 162 s = set() 163 s.add(5) 164 test() 165 print(s) 166 # 上面代码结果是:{5}

 字典 、 数组、集合、元组,都是全局变量?

-----------------------------------------Have a good day!---------------------------------------------------------------------------------------------------
原文地址:https://www.cnblogs.com/ww-xiaowei/p/11231993.html