博客
关于我
06-局部变量和全局变量
阅读量:595 次
发布时间:2019-03-11

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

局部变量:在函数内部定义的变量,只是在当前函数中使用,在其他函数就不行

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'}

 

转载地址:http://kpltz.baihongyu.com/

你可能感兴趣的文章
MySQL:什么样的字段适合加索引?什么样的字段不适合加索引
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
MySQL:某个ip连接mysql失败次数过多,导致ip锁定
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
m_Orchestrate learning system---二十二、html代码如何变的容易
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
nacos config
查看>>
NacosClient客户端搭建,微服务注册进nacos
查看>>
Nacos原理
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos如何实现Raft算法与Raft协议原理详解
查看>>
Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Nacos注册中心有几种调用方式?
查看>>
nacos注册失败,Feign调用失败,feign无法注入成我们的bean对象
查看>>
nacos源码 nacos注册中心1.4.x 源码 nacos源码如何下载 nacos 客户端源码下载地址 nacos discovery下载地址(一)
查看>>
Nacos编译报错NacosException: endpoint is blank
查看>>
NACOS部署,微服务框架之NACOS-单机、集群方式部署
查看>>
Nacos配置中心集群原理及源码分析
查看>>
nacos配置新增不成功
查看>>
nacos配置自动刷新源码解析
查看>>