8.18 python复习

8.12 NumPy 数组索引 (w3school.com.cn)

8.18

1、

__iter__() 方法的作用相似,您可以执行操作(初始化(_init_())等),但必须始终返回迭代器对象本身。

__next__() 方法也允许您执行操作,并且必须返回序列中的下一个项目。

class MyNumbers:
  def __iter__(self):
    self.a = 1              # a 为属性
    return self

  def __next__(self):
   if self.a <=20: 
    x = self.a    self.a += 1    return x # x为对象的返回值   else:
    raise StopIteration
myclass = MyNumbers()
myiter = iter(myclass)

print(next(myiter))      #1
print(next(myiter))      #2
print(next(myiter))      #3
print(next(myiter))      #4
print(next(myiter))      #5    
…… #20

#for x in myiter:
#  print(x)

 2、 迭代

mytuple=("1","2")

myit=iter(mytuple)

print(next(myit))

=

mytuple=("1","2")

for x in mytuple:

print(x)

3、global: 使局部变量成为全局变量;在函数内部更改全局变量x的值,则可global x引用

 

posted @ 2021-08-23 16:34  衣囧~  阅读(39)  评论(0)    收藏  举报