python: iter_method

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-06-26 01:00:00 +00:00
parent abf274ab7a
commit 4477b1b941
2 changed files with 16 additions and 0 deletions

View File

@@ -18482,6 +18482,7 @@ Examples:
* link:rootfs_overlay/lkmc/python/hello.py[]: hello world
* `time`
** link:rootfs_overlay/lkmc/python/count.py[]: count once every second
** link:rootfs_overlay/lkmc/python/iter_method.py[]: how to implement `__iter__` on a class
===== Build and install the interpreter

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env python3
# https://cirosantilli.com/linux-kernel-module-cheat#python
class MyClass():
def __init__(self, mylist):
self.mylist = mylist
def __iter__(self):
return iter(self.mylist)
mylist = [1, 3, 2]
i = 0
for item in MyClass(mylist):
assert item == mylist[i]
i += 1