2019-11-15

# 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

# # 创建Connection连接

# conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='123456',charset='utf8')

# # 获得Cursor对象

# cursor = conn.cursor()

# count = cursor.execute("select * from goods;")

# print("查询到%d条数据" % 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)

# # 关闭Cursor对象

# cursor.close()

# conn.close()

# # print(cursor.fetchone(), "---->");

# # count = cursor.execute("select * from goods;")

# from pymysql import connect

# # 创建Connection连接

# conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='123456',charset='utf8')

# # 获得Cursor对象

# cs1 = conn.cursor()

# # print(cs1.execute("""insert into goods_cates (name) values ("硬盘")"""))

# # print(cs1.execute("""insert into goods_cates (name) values ("硬盘2")"""))

# # print(cs1.execute("""insert into goods_cates (name) values ("硬盘3")"""))

# # conn.commit()

# print(cs1.execute("""insert into goods_cates (name) values ("硬盘3")"""))

# print(cs1.execute("""insert into goods_cates (name) values ("硬盘4")"""))

# conn.rollback()

# print(cs1.execute("""insert into goods_cates (name) values ("硬盘4")"""))

# conn.commit()

# import urllib.parse

#

# print(urllib.parse.quote("@"))

# print(urllib.parse.quote("中国"))

#

# 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):

"""如果想要一个对象成为一个可以迭代的对象(即可以使用for),那么必须实现__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是否是可以迭代的对象:", 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# 如果一个函数中有yield语句,那么这个就不再是函数,而是一个生成器的模板

    print("---3---")

a, b = b, a+b

current_num +=1

    print("---4---")

# 如果在调用create_num的时候,发现这个函数中有yield,那么此时不是调用函数,而是创建一个生成器对象

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# 如果一个函数中有yield语句,那么这个就不再是函数,而是一个生成器的模板

    a, b = b, a+b

current_num +=1

  return "ok~~~"

# 如果在调用create_num的时候,发现这个函数中有yield,那么此时不是调用函数,而是创建一个生成器对象

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():

# 此时并非函数调用,而是创建生成器

  t1 = task_1()

t2 = task_2()

# 先让t1运行一会儿,当t1中遇到yield的时候,再返回到这里

  # 然后执行t2,当它遇到yield的时候,再次切换到t1中

  # 这样t1/t2/t1/t2的交替运行,最终实现了多任务……协程

  while True:

next(t1)# 调用task_1,yield时返回

    next(t2)# 调用task_2,yield时返回

if __name__ =="__main__":

main()

你可能感兴趣的:(2019-11-15)