您当前的位置: 首页 >  Python
  • 3浏览

    0关注

    2393博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python语言学习之变量那些事:局部变量、全局变量的使用方法之详细攻略

一个处女座的程序猿 发布时间:2019-07-22 07:46:06 ,浏览量:3

Python语言学习之变量那些事:局部变量、全局变量的使用方法之详细攻略

 

 

 

 

目录

变量那些事

1、判断变量test,是否已经被定义

2、全局变量和局部变量

 

 

 

 

 

变量那些事 1、判断变量test,是否已经被定义
#判断变量test,是否已经被定义
res1 = 'test' in locals().keys()
res2 = 'test' in dir()
res3 = 'test' in vars().keys()
print(res1,res2,res3)  # 变量test暂时还没有定义,返回False
test = ""  # 定义变量test
res4 = 'test' in locals().keys()
res5 = 'test' in dir()
res6 = 'test' in vars().keys()
print(res4,res5,res6)  # 变量test已经被定义了,返回True

False False False
True True True

 

2、全局变量和局部变量

(1)、定义内部函数修改外部的全局变量

def Change_global(): #WeChat_sign02变量更改WeChat_sign01变量
    WeChat_sign02=0
    global WeChat_sign01
    if WeChat_sign02==0:
        WeChat_sign01=1

WeChat_sign01=0
Change_global()
print(WeChat_sign01)

 

 

 

 

 

 

关注
打赏
1664196048
查看更多评论
立即登录/注册

微信扫码登录

0.1413s