06-局部变量和全局变量
发布日期:2021-05-14 21:58:02 浏览次数:8 分类:博客文章

本文共 4659 字,大约阅读时间需要 15 分钟。

���������������������������������������������������������������������������������������������������������

In [1]: def test1():������������#������������test1������   ...:     a = 100������#������   ...:     In [2]: def test2():������������#������������test2������   ...:     print("a=%d"%a)������#������   ...:
In [4]: test1()������#������test1���������������������return���������������������In [5]: test2()������#������test2���������������---------------------------------------------------------------------------NameError                                 Traceback (most recent call last)
in
()----> 1 test2()
in test2() 1 def test2():----> 2 print("a=%d"%a) 3 NameError: name 'a' is not defined������#������a���������������a������test1������������������������������

������������������������������������������������������������������

In [1]: a = 99������������#������������������������������������������������������������In [2]: def test1():   ...:     a = 100   ...:     print("test1���������a=%d"%a)   ...:     In [3]: def test2():   ...:     print("test2���������a=%d"%a)   ...:     In [4]: test1()test1���������a=100In [5]: test2()test2���������a=99

���������������test1���������a������������100������������������������������������������������������������������������������������������������������������������

global������#������������������

root@ubuntu:/home/python/codes/python������-05# cat !$cat 03-������������.pywendu = 30������#���������������������������wendudef get_wendu():    wendu = 33������#���wendu������������def print_wendu():    print("������������������%d"%wendu)������#������get_wendu()������#������������print_wendu()������#������������root@ubuntu:/home/python/codes/python������-05# python3 !$python3 03-������������.py������������������30������#���������30

���������������������������������������������wendu���������������������������������������������������30������������������wendu������������������������������������������������������������������������������������������������������������������������������wendu=������������������������������wendu���������������������������������������������������������������������������������������������������

root@ubuntu:/home/python/codes/python������-05# cat !$cat 03-������������.pywendu = 30def get_wendu():    global wendu������#global wendu���������������������������������������������wendu=33������������������������������    wendu = 33def print_wendu():    print("������������������%d"%wendu)get_wendu()print_wendu()root@ubuntu:/home/python/codes/python������-05# python3 !$python3 03-������������.py������������������33

������������������������������������������������������������������������������������������global���������

���������������������������

1.������������������������������������������***

2.������������������

���������������������

������������������������������������������������������

In [1]: def test():   ...:     print("abcdefg")   ...:     In [2]: help(test)Help on function test in module __main__:test()

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

In [3]: help(print)Help on built-in function print in module builtins:print(...)    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)        Prints the values to a stream, or to sys.stdout by default.    Optional keyword arguments:    file:  a file-like object (stream); defaults to the current sys.stdout.    sep:   string inserted between values, default a space.    end:   string appended after the last value, default a newline.    flush: whether to forcibly flush the stream.

���������������print���������������������������������������test���������������������������

In [4]: def test():   ...:     '''���������������'''������������#������������������������������������������������������   ...:     print("abcdef")   ...:     In [5]: help(test)Help on function test in module __main__:test()    ���������������

���������������������������������

������������������������������������������������������������������global������������python������������������������������������������

root@ubuntu:/home/python/codes/python������-05# cat test.py #-*- coding:utf-8 -*-nums = [11,22,33]infor = {"name":"laowang","age":16}def test1():    nums.append(44)    infor['addr']= "henan"def test2():    print(nums)    print(infor)test1()test2()root@ubuntu:/home/python/codes/python������-05# python3 test.py [11, 22, 33, 44]{'addr': 'henan', 'age': 16, 'name': 'laowang'}

 

上一篇:牛逼!Java 从入门到精通,超全汇总版
下一篇:05-python中函数的使用

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月27日 08时48分49秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章