def __init__(self,start,stop):
self.value=start-1
self.stop=stop
def __iter__(self):
return self
def __next__(self):
if self.value==self.stop:
raise StopIteration
self.value+=1
return self.value*2
d=Double(1,5)
for i in d:
print(i,end=" ")
2 4 6 8 10 当对象被索引(单下标索引或切片索引),或执行获取相关的操作(如for循环迭代访问)时,Python会自动调用__getitem__()方法;当为索引或切片赋值时,Python会自动调用__setitem__()方法。根据Python的迭代协议,iter()函数对应魔法方法__iter__(),定义了该方法的对象就是可迭代对象;next()函数对应魔法方法__next__(),定义了该方法的可迭代对象就是迭代器。知晓原理后,我们就可以用while循环来手动模拟for循环,还可以对这两个魔法方法进行重写,实现自己的迭代器!除此之外,我们还知道了切片操作其实是BIF函数slice()的一个语法糖,写起来更加简便,也更易读! class C:
def __getitem__(self,index):
print(index)
c = C()
c
2
c
slice(2, 8, None)
s = "I love FishC"
s
'love'
s
'love'
s
'FishC'
s
'FishC'
s[::4]
'Ivi'
s
'Ivi'
s
'Ivi'
class D:
def __init__(self,data):
self.data = data
def __getitem__(self,index):
return self.data
def __setitem__(self,index,value):
self.data = value
d = D()
d
2
d = 1
d
1
d =
d[:]
class D:
def __init__(self,data):
self.data = data
def __getitem__(self,index):
return self.data * 2
d = D()
for i in d:
print(i,end='')
246810
x =
next(x)
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
next(x)
TypeError: 'list' object is not an iterator
for i in x:
print(i,end=' ')
1 1 2 3 5
_ = iter(x)
while True:
try:
i = _.__next__()
except StopIteration:
break
print(i,end=' ')
1 1 2 3 5
class Double:
def __init__(self,start,stop):
self.value = start - 1
self.stop = stop
def __iter__(self):
return self
def __next__(self):
if self.value == self.stop:
raise StopIteration
self.value += 1
return self.value * 2
d = Double(1,5)
for i in d:
print(i,end=' ')
2 4 6 8 10
本帖最后由 Ensoleile 于 2023-1-10 00:32 编辑
索引、切片、迭代协议
#__getitem__(self, index):当对象被索引时调用,既能相应单个下标的索引操作,又能支持代表范围的切片索引
class C:
def __getitem__(self, item):
print(item)
c =C()
c#2
c#slice(2, 8, None)
#slice()为BIF函数,切片操作相当于他的一个语法糖
s = 'i love you'
print(s)#love
print(s)#love
print(s, s)#you you
print(s[::4], s)#ivo ivo
#__setitem__():为索引、切片赋值时调用
class D:
def __init__(self, data):
self.data = data
def __getitem__(self, item):
return self.data
def __setitem__(self, key, value):
self.data = value
d = D()
print(d)#2
d = 1
print(d)#1
d =
print(d[:])#
#__getitem__()不仅拦截索引或切片,另外与获取相关的操作也会被拦截
class D:
def __init__(self, data):
self.data = data
def __getitem__(self, item):
return self.data * 2
d = D()
for i in d:
print(i, end=' ')#2 4 6 8 10
#for语句i每次获取都会访问__getitem__()方法,使结果加一
#针对可迭代对象的魔法方法:__iter__()和__next__()
#根据python的迭代协议来说,如果一个对象定义了__iter__()魔法方法,那么他就是一个可迭代对象,如果一个可迭代对象定义了__next()__魔法方法,那么他就是一个迭代器
x =
print(dir(x))#dir()函数返回对象的所有属性和方法,不带值
#['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
#没有__next__(),列表不是一个迭代器
for i in x:
print(i, end=' ')#1 1 2 3 5
#当使用迭代工具(如for语句)对可迭代对象进行操作的时候,①先将对象传入内置函数iter()中,并由此拿到一个相应的迭代器,因为只有迭代器才能拥有所需的__next__()魔法方法,②再利用__next__()魔法方法,进行真正的迭代操作
#模拟for语句循环流程如下:
_ = iter(x)
while True:
try:
i = _.__next__()
except StopIteration:
break
print(i, end=' ')#1 1 2 3 5
#创造一个迭代器对象
class Double:
def __init__(self, start, stop):
self.value = start - 1
self.stop = stop
def __iter__(self):
return self
def __next__(self):
if self.value == self.stop:
raise StopIteration
self.value += 1
return self.value * 2
d = Double(1, 5)
print(type(d))#<class '__main__.Double'>
print(next(d))#2
print(next(d))#4
for i in d:
print(i, end=' ')#6 8 10
#对象d可以实现迭代器相同的操作 没有笔记过不下去了。。。。。。。。。。。。。。。。 ## \_\_getitem__(self, index)
当对象被索引的时候, python会调用`__getitem__`,它既能响应单个下标的索引操作,又能支持代表范围的切片索引的方式
```python
class C:
def __getitem__(self, index):
print(index)
c = C()
c
2
c
slice(2, 8, None)
```
slice()是一个BIF函数,切片操作是他的语法糖
```python
s = "I love you"
s
'love'
s
'love'
s
'you'
s
'you'
s[::4]
'Ivo'
s
'Ivo'
```
## \_\_setitem__(self, key, value)
为索引或切片赋值操作的时候就会被setitem方法拦截
```python
class D:
def __init__(self, data):
self.data = data
def __getitem__(self, index):
return self.data
def __setitem__(self, index, value):
self.data = value
d = D()
d
2
d = 1
d
1
d =
d[:]
```
如果迭代器是基于索引的(例如,使用一个索引来迭代列表元素),那么会触发该对象的 `__getitem__` 方法
```python
class D:
def __init__(self, data):
self.data = data
def __getitem__(self, index):
return self.data * 2
def __setitem__(self, index, value):
self.data = value
d = D()
for i in d:
print(i, end=' ')
2 4 6 8 10
```
## \_\_iter__(self)和\_\_next__(self)
用for语句去访问`__getitem()__`是python退而求其次的方法
如果一个对象定义了`__iter__()`魔法方法那么他就是一个可迭代对象,定义了`__next__()`则是一个迭代器
比如说,列表是一个可迭代对象,他不是一个迭代器,他没有next方法
```python
x =
dir(x)
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
next(x)
Traceback (most recent call last):
File "<pyshell#111>", line 1, in <module>
next(x)
TypeError: 'list' object is not an iterator
```
可迭代对象定义了`__iter()__`魔法方法,调用就会得到一个相应的迭代器对象
使用for对一个可迭代对象操作时,先将对象传入内置函数iter()中并拿到一个相应的迭代器,才能拥有`__next__()`方法,再利用`__next__()`方法进行迭代操作,当抛出StopIteration时到达尽头
```python
_ = iter(x)
while True:
try:
i = _.__next__()
except StopIteration:
break
print(i, end = ' ')
1 1 2 3 5
```
创造一个迭代器对象
```python
class Double:
def __init__(self, start, stop):
self.value = start - 1
self.stop = stop
def __iter__(self):
return self
def __next__(self):
if self.value == self.stop:
raise StopIteration
self.value += 1
return self.value * 2
d = Double(1, 5)
for i in d:
print(i, end = ' ')
2 4 6 8 10
```
class C:
def __getitem__(self,index):
print(index)
c = C()
c
2
c
slice(2, 8, None)
s = "I love FishC"
s
'love'
s
'love'
s
'FishC'
s
'FishC'
s[::4]
'Ivi'
s
'Ivi'
s
'Ivi'
class D:
def __init__(self,data):
self.data = data
def __getitem__(self,index):
return self.data
def __setitem__(self,index,value):
self.data = value
d = D()
d
2
d = 1
d
1
d =
d[:]
class D:
def __init__(self,data):
self.data = data
def __getitem__(self,index):
return self.data * 2
d = D()
for i in d:
print(i,end='')
246810
x =
next(x)
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
next(x)
TypeError: 'list' object is not an iterator
for i in x:
print(i,end=' ')
1 1 2 3 5
_ = iter(x)
while True:
try:
i = _.__next__()
except StopIteration:
break
print(i,end=' ')
1 1 2 3 5
class Double:
def __init__(self,start,stop):
self.value = start - 1
self.stop = stop
def __iter__(self):
return self
def __next__(self):
if self.value == self.stop:
raise StopIteration
self.value += 1
return self.value * 2
d = Double(1,5)
for i in d:
print(i,end=' ')
2 4 6 8 10
页:
[1]