-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobalExample.py
More file actions
64 lines (52 loc) · 1.13 KB
/
Copy pathglobalExample.py
File metadata and controls
64 lines (52 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3
#coding=utf-8
#global全局变量
#用global之后就可以修改外面的变量
a='6'
def fun():
global a
a = '7'
print (a)
fun()
print (a)
#如果不用,那么修改的只是内部的变量跟外部的变量没关系
def func_local(x):
print ('x is', x)
x = 2
print ('Chanaged local x to',x)
x = 50
func_local(x)
print ('x is still', x)
#函数参数若是list、set、dict可变参数,在函数内改变参数,会导致该参数发生变化,例如:
#(1)
def func_local(x):
print ('x is', x)
x.append(10)
print ('Chanaged local x to',x)
x = range(6)
func_local(x)
print ('x is', x)
#(2)
def func_local(x):
print ('x is', x)
x.add(10)
print ('Chanaged local x to',x)
x = set(range(6))
func_local(x)
print ('x is', x)
#(3)
def func_local(x):
print ('x is', x)
x.add(10)
print ('Chanaged local x to',x)
x = set(range(6))
func_local(x)
print ('x is', x)
#如果是元组的话,就不行了,改变不了外面的
def func_local(x):
print ('x is', x)
x = (4, 5, 6)
print ('Chanaged local x to',x)
x = (1,2,3,)
func_local(x)
print ('x is', x)