Python 之 使用全局变量
2010-09-27 16:37
127 查看
这里定义了两个全局的变量countx 和 county
#!/usr/bin/env python
#__metaclass__ = type
nest = [[1,2],[3,4],5]
countx = 0
county = 0
def flatten(nested):
try:
for sublist in nested:
print 'sublist=',sublist
# global countx
# print 'countx = %i' % countx
# countx += 1
for element in flatten(sublist):
print 'element=',element
# global county
# print 'county = %i' % county
# county += 1
yield element
except TypeError:
print 'except'
# print countx,county
yield nested
#!/usr/bin/env python
#__metaclass__ = type
nest = [[1,2],[3,4],5]
countx = 0
county = 0
def flatten(nested):
try:
for sublist in nested:
print 'sublist=',sublist
# global countx
# print 'countx = %i' % countx
# countx += 1
for element in flatten(sublist):
print 'element=',element
# global county
# print 'county = %i' % county
# county += 1
yield element
except TypeError:
print 'except'
# print countx,county
yield nested
相关文章推荐
- 讨论下python中全局变量的使用
- python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
- python使用全局变量(global)
- python3 动态模块导入与全局变量使用
- (原)python中不同文件之间使用所谓的全局变量
- python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
- Python基础学习篇——Global全局变量的使用
- python全局变量使用
- 关于Python中全局变量的使用的调试
- python unittest TestCase间共享数据(全局变量的使用)
- python多文件使用同一个全局变量的定义和使用方法
- Python类方法、静态方法、全局变量的使用
- Python 全局变量的使用
- python变量总结: 全局变量、局部变量、类变量、实例变量以及global和nonlocal关键字的使用示例
- 5.1 Python 函数-默认参数,*args,**kwargs的使用;作用域-局部空间使用全局变量需要注意的事项等
- python全局变量使用注意事项_
- Python 3 实现定义跨模块的全局变量和使用
- python 全局变量的使用
- Python: 全局变量 & 局部变量的使用
- python中全局变量的使用