码迷,mamicode.com
首页 > 编程语言 > 详细

python globle用法

时间:2017-11-29 18:10:59      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:reference   错误   assign   python   global   oca   不能   error:   操作   

函数作用域分为

  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用法

标签:reference   错误   assign   python   global   oca   不能   error:   操作   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!