# for temp in [11, 22, 33]:
#? print(temp)
# for temp in "abcdef":
#? print(temp)
# for temp in 100:
#? print(temp)
# from collections import Iterable
# print(isinstance([11, 22, 33], Iterable))
# print(isinstance((11, 22, 33), Iterable))
# print(isinstance("abc", Iterable))
# print(isinstance(100, Iterable))
# a = 0
# b = 1
# print(a)
# a, b = b, a+b? # (1, 0+1)
# print(a)
# a, b = b, a+b? # (1, 0+1)
# print(a)
# a, b = b, a+b? # (1, 0+1)
# print(a)
# a = (11, 22, 33)
# print(list(a))
# nums = [x*2 for x in range(10)]
# print(nums)
# nums = (x*2 for x in range(10))
# print(nums)
# for num in nums:
#? print(num)
# import urllib.request
# req = urllib.request.urlopen("http://www.baidu.com")
# content = req.read()
# print(content)
# Python2適用
# import MySQLdb
# Python3適用
# import pymysql
# from pymysql import connect
# # 創(chuàng)建Connection連接
# conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='123456',charset='utf8')
# # 獲得Cursor對(duì)象
# cursor = conn.cursor()
# count = cursor.execute("select * from goods;")
# print("查詢(xún)到%d條數(shù)據(jù)" % count)
# # print(cursor.fetchone());
# # print(cursor.fetchone());
# # print(cursor.fetchone());
# # print(cursor.fetchone());
# # print(cursor.fetchmany());
# # print(cursor.fetchmany(3));
# # print(cursor.fetchall());
# # print(cursor.fetchall());
# # line_content = cursor.fetchone();
# # print(line_content)
# # print(line_content[0])
# # print(line_content[1])
# # print(line_content[2])
# # for temp in line_content:
# #? print(temp)
# lines = cursor.fetchmany(5);
# for temp in lines:
#? print(temp)
# # 關(guān)閉Cursor對(duì)象
# cursor.close()
# conn.close()
# # print(cursor.fetchone(), "---->");
# # count = cursor.execute("select * from goods;")
# from pymysql import connect
# # 創(chuàng)建Connection連接
# conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='123456',charset='utf8')
# # 獲得Cursor對(duì)象
# cs1 = conn.cursor()
# # print(cs1.execute("""insert into goods_cates (name) values ("硬盤(pán)")"""))
# # print(cs1.execute("""insert into goods_cates (name) values ("硬盤(pán)2")"""))
# # print(cs1.execute("""insert into goods_cates (name) values ("硬盤(pán)3")"""))
# # conn.commit()
# print(cs1.execute("""insert into goods_cates (name) values ("硬盤(pán)3")"""))
# print(cs1.execute("""insert into goods_cates (name) values ("硬盤(pán)4")"""))
# conn.rollback()
# print(cs1.execute("""insert into goods_cates (name) values ("硬盤(pán)4")"""))
# conn.commit()
# import urllib.parse
#
# print(urllib.parse.quote("@"))
# print(urllib.parse.quote("中國(guó)"))
#
# print(urllib.parse.unquote("%E4%B8%AD%E5%9B%BD"))
import time
from collectionsimport Iterable
from collectionsimport Iterator
class Classmate(object):
def __init__(self):
self.names = list()
self.current_num =0
? def add(self, name):
self.names.append(name)
def __iter__(self):
"""如果想要一個(gè)對(duì)象成為一個(gè)可以迭代的對(duì)象(即可以使用for),那么必須實(shí)現(xiàn)__iter__方法"""
? ? # return ClassIterator(self)
? ? return self
? def __next__(self):
if self.current_num < len(self.names):
ret =self.names[self.current_num]
self.current_num +=1
? ? ? return ret
else:
raise StopIteration
# class ClassIterator(object):
#? def __init__(self, obj):
#? ? self.obj = obj
#? ? self.current_num = 0
#? def __next__(self):
#? ? if self.current_num < len(self.obj.names):
#? ? ? ret = self.obj.names[self.current_num]
#? ? ? self.current_num += 1
#? ? ? return ret
#? ? else:
#? ? ? raise StopIteration
classmate = Classmate()
classmate.add("宏杰")
classmate.add("永振")
classmate.add("明越")
# print("判斷classmate是否是可以迭代的對(duì)象:", isinstance(classmate, Iterable))
# classmate_iterator = iter(classmate)
# print("判斷classmate_iterator是否是迭代器:", isinstance(classmate_iterator, Iterator))
# print(next(classmate_iterator))
for namein classmate:
print(name)
time.sleep(1)
"""
nums = list()
a = 0
b = 1
i = 0
while i < 10:
nums.append(a)
a, b = b, a+b
i += 1
for num in nums:
print(num)
"""
class Fibonacci(object):
def __init__(self, all_num):
self.all_num = all_num
self.current_num =0
? ? self.a =0
? ? self.b =1
? def __iter__(self):
return self
? def __next__(self):
if self.current_num
ret =self.a
self.a, self.b =self.b, self.a+self.b
self.current_num +=1
? ? ? return ret
else:
raise StopIteration
fibo = Fibonacci(10)
for numin fibo:
print(num)
def create_num(all_num):
print("---1---")
# a = 0
# b = 1
? a, b =0, 1
? current_num =0
? while current_num < all_num:
print("---2---")
# print(a)
? ? yield a# 如果一個(gè)函數(shù)中有yield語(yǔ)句仰坦,那么這個(gè)就不再是函數(shù)睡陪,而是一個(gè)生成器的模板
? ? print("---3---")
a, b = b, a+b
current_num +=1
? ? print("---4---")
# 如果在調(diào)用create_num的時(shí)候娶耍,發(fā)現(xiàn)這個(gè)函數(shù)中有yield蚪腋,那么此時(shí)不是調(diào)用函數(shù)柬赐,而是創(chuàng)建一個(gè)生成器對(duì)象
obj = create_num(10)
obj2 = create_num(2)
ret = next(obj)
print("obj:", ret)
ret = next(obj)
print("obj:", ret)
ret = next(obj2)
print("obj2:", ret)
ret = next(obj)
print("obj:", ret)
ret = next(obj)
print("obj:", ret)
ret = next(obj)
print("obj:", ret)
ret = next(obj2)
print("obj2:", ret)
ret = next(obj2)
print("obj2:", ret)
# for num in obj:
#? ? print(num)
def create_num(all_num):
# a = 0
# b = 1
? a, b =0, 1
? current_num =0
? while current_num < all_num:
# print(a)
? ? yield a# 如果一個(gè)函數(shù)中有yield語(yǔ)句妒蛇,那么這個(gè)就不再是函數(shù)倦挂,而是一個(gè)生成器的模板
? ? a, b = b, a+b
current_num +=1
? return "ok~~~"
# 如果在調(diào)用create_num的時(shí)候龄砰,發(fā)現(xiàn)這個(gè)函數(shù)中有yield敷鸦,那么此時(shí)不是調(diào)用函數(shù),而是創(chuàng)建一個(gè)生成器對(duì)象
obj2 = create_num(20)
while True:
try:
ret = next(obj2)
print(ret)
except Exceptionas ret:
print(ret.value)
break
def create_num(all_num):
a, b =0, 1
? ? current_num =0
? ? while current_num < all_num:
ret =yield a
print(">>>ret>>>", ret)
a, b = b, a+b
current_num +=1
obj = create_num(10)
obj.send(None)
ret = next(obj)
print(ret)
ret = obj.send("hahaha")
print(ret)
import time
def task_1():
while True:
print("---1---")
time.sleep(0.1)
yield
def task_2():
while True:
print("---2---")
time.sleep(0.1)
yield
def main():
# 此時(shí)并非函數(shù)調(diào)用寝贡,而是創(chuàng)建生成器
? t1 = task_1()
t2 = task_2()
# 先讓t1運(yùn)行一會(huì)兒扒披,當(dāng)t1中遇到y(tǒng)ield的時(shí)候,再返回到這里
? # 然后執(zhí)行t2圃泡,當(dāng)它遇到y(tǒng)ield的時(shí)候碟案,再次切換到t1中
? # 這樣t1/t2/t1/t2的交替運(yùn)行,最終實(shí)現(xiàn)了多任務(wù)……協(xié)程
? while True:
next(t1)# 調(diào)用task_1颇蜡,yield時(shí)返回
? ? next(t2)# 調(diào)用task_2价说,yield時(shí)返回
if __name__ =="__main__":
main()