import pdb # 导入pdb代码调试模块
def func(x):
pdb.set_trace() # 使用代码调试
print 'x is', x
x = 2
print 'Changed local variable x to', x
# --[空行] -------
x = 50 # 其在函数之外,是全局变量,可以被文件内部的任何函数、任何代码段访问、外部文件也可以访问
func(x)
print 'x is still', x
''' 输出结果 '''
>>>
x is 50
Changed local x to 2
x is still 50
''' -----------代码调试阶段-------------'''
''' 其中l => list 显示运行代码 ,n = > next 执行下一行,既一步一步执行'''
>>> ================================ RESTART ================================
>>>
> d:\python27\lab\3.py(4)func()
-> print 'x is', x
(Pdb) l
1 import pdb
2 def func(x):
3 pdb.set_trace()
4 -> print 'x is', x
5 x = 2
6 print 'Changed local x to', x
7
8 x = 50
9 func(x)
10 print 'x is still', x
[EOF]
(Pdb) n
x is 50 # 由于x=50是全局变量,被函数中 print 'x is', x调用
> d:\python27\lab\3.py(5)func()
-> x = 2
(Pdb) l
1 import pdb
2 def func(x):
3 pdb.set_trace()
4 print 'x is', x
5 -> x = 2
6 print 'Changed local x to', x
7
8 x = 50
9 func(x)
10 print 'x is still', x
[EOF]
(Pdb) n
> d:\python27\lab\3.py(6)func()
-> print 'Changed local x to', x
(Pdb) l
1 import pdb
2 def func(x):
3 pdb.set_trace()
4 print 'x is', x
5 x = 2
6 -> print 'Changed local x to', x
7
8 x = 50
9 func(x)
10 print 'x is still', x
[EOF]
(Pdb) n
Changed local x to 2 # x=2是函数中的局部变量,被print 'Changed local x to', x调用
--Return--
> d:\python27\lab\3.py(6)func()->None
-> print 'Changed local x to', x
(Pdb) l
1 import pdb
2 def func(x):
3 pdb.set_trace()
4 print 'x is', x
5 x = 2
6 -> print 'Changed local x to', x
7
8 x = 50
9 func(x)
10 print 'x is still', x
[EOF]
(Pdb) n
> d:\python27\lab\3.py(10)()
-> print 'x is still', x
(Pdb) l
5 x = 2
6 print 'Changed local x to', x
7
8 x = 50
9 func(x)
10 -> print 'x is still', x
[EOF]
(Pdb) n
x is still 50 # 由于x=50是全局变量,被 print 'x is still', x调用
--Return--
> d:\python27\lab\3.py(10)()->None
-> print 'x is still', x
(Pdb) l
5 x = 2
6 print 'Changed local x to', x
7
8 x = 50
9 func(x)
10 -> print 'x is still', x
[EOF]
(Pdb) n
> d:\python27\lib\idlelib\run.py(300)runcode()
-> interruptable = False
(Pdb) q
'''----global---字段,在函数内声明变量为全局变量'''
def func(): # 此函数没有定义形参,在后面的函数调用中也就没有必要传入实参,仅仅是定义了一个func()函数而已
global x # 声明x是全局变量,并引用全局变量
print 'x is', x # 将全局变量x =50,中的‘50’引入print中
x = 2 # 声明局部变量
print 'Changed local x to', x # 将局部变量x =2中的’2‘引入print中
x = 50 # 在函数外定义的,即为全局变量
func() # 调用func()函数
print 'Value of x is', x # 打印x=50
>>>
x is 50
Changed local x to 2
Value of x is 2
阅读(1692) | 评论(0) | 转发(0) |