mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
python: iter_method
This commit is contained in:
@@ -18482,6 +18482,7 @@ Examples:
|
|||||||
* link:rootfs_overlay/lkmc/python/hello.py[]: hello world
|
* link:rootfs_overlay/lkmc/python/hello.py[]: hello world
|
||||||
* `time`
|
* `time`
|
||||||
** link:rootfs_overlay/lkmc/python/count.py[]: count once every second
|
** 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
|
===== Build and install the interpreter
|
||||||
|
|
||||||
|
|||||||
15
rootfs_overlay/lkmc/python/iter_method.py
Executable file
15
rootfs_overlay/lkmc/python/iter_method.py
Executable 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
|
||||||
Reference in New Issue
Block a user