0
点赞
收藏
分享

微信扫一扫

Python爬虫-第二章-5-函数

  1. 在局部中引入外部变量进行修改

# Demo Describe:在局部中引入外部变量进行修改

# ------1.global-------------
a = 10


def funTest1():
global a # 引入全局变量
a = 20


fun = funTest1
fun()
print(a) # 20


# ------2.nonlocal-------------

def funTest2():
a = 20

def funTest3():
nonlocal a # 引入外层局部变量
a = 30

funTest3()
print(a)


fun = funTest2
fun() # 30

  1. 迭代器

# Demo Describe:迭代器

'''
iterable:可迭代
str,list,tuple,dict,set,open()
*可迭代数据类型都有默认的迭代器,可将数据逐一取出
迭代器的获取
1.iter()
2.__iter__()
迭代器数据获取
1.next()
2.__next__()
迭代器作用
常用于for循环,循环中使用迭代器对各种可迭代数据类型进行遍历操作
for 变量 in iterable:
pass
结论:统一不同可迭代数据类型的遍历工作
本章内容:
xxxxx
xxxx
'''

# start--------1,简单示例----------------------

# 1
# a = '你好世界!'
# res = iter(a)
# print(next(res))
# print(next(res))

# 2
a = '你好世界!'
res = a.__iter__()
print(res.__next__())
print(res.__next__())
print(res.__next__())

# end--------1,简单示例----------------------

举报

相关推荐

0 条评论