首页 > 编程语言 > 详细

python globle用法

时间:2017-11-29 17:46:02      阅读:399      评论:0      收藏:0      [点我收藏+]

函数作用域分为

  1. 全局  在整个类和模块内有作用

  2.局部  在函数内部优先于全局

在函数内部可以调用全局变量,但是不能修改其值(因为python中定义和修改操作一致),这是需要用到 globle 声明操作变量为全局变量

 

e.g.  正确:

hehe=6
def f():
global hehe
print(hehe)
hehe=3
f()
print(hehe)

e.g. 错误:
hehe=6
def f():
print(hehe)
hehe=2
f()
print(hehe)
# 会报错: 未定义变量 UnboundLocalError: local variable ‘hehe‘ referenced before assignment

python globle用法

原文:http://www.cnblogs.com/lihuanghe/p/7921617.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!