0
点赞
收藏
分享

微信扫一扫

python 全局变量

像小强一样活着 2022-05-03 阅读 92
python

参考博客
https://blog.csdn.net/cunchi4221/article/details/107475684

测试案例1

website = "JournalDev.com"
def return_website():
    return website
print(f'My favorite website is {return_website()}')

结果如下:
在这里插入图片描述# 测试案例2

website = "JournalDev.com"
def print_website():
    global website
    print(f'My favorite website is {website}')
 
    website = 'Wikipedia.com'
    print(f'My favorite website is {website}')
print_website()

在这里插入图片描述

测试案例3

在这里插入图片描述结果如下
在这里插入图片描述

测试案例4(我想对函数调用的次数进行计数,我记得在C++里是很好实现的

pythotn的做法如下

cnt=-1 

def f():
    global cnt
    cnt=cnt+1
    
f()
print(cnt)

测试结果如下
在这里插入图片描述

举报

相关推荐

0 条评论