mirror of
https://github.com/tengge1/ShadowEditor.git
synced 2026-01-25 15:08:11 +00:00
iterators
This commit is contained in:
parent
e3e459d44f
commit
f61eda145a
20
test/python/class_test.py
Normal file
20
test/python/class_test.py
Normal file
@ -0,0 +1,20 @@
|
||||
class MyClass:
|
||||
'''A simple example class'''
|
||||
i = 12345
|
||||
|
||||
def __init__(self):
|
||||
self.i = 123
|
||||
|
||||
def f(self):
|
||||
print(self.i)
|
||||
|
||||
|
||||
class DerivedClass(MyClass):
|
||||
pass
|
||||
|
||||
x = DerivedClass()
|
||||
x.f()
|
||||
|
||||
x.name = 'Test'
|
||||
|
||||
print(x.name)
|
||||
44
test/python/iterators.py
Normal file
44
test/python/iterators.py
Normal file
@ -0,0 +1,44 @@
|
||||
for i in [1, 2, 3]:
|
||||
print(i)
|
||||
|
||||
for i in (4, 5, 6):
|
||||
print(i)
|
||||
|
||||
for i in {'a': 1, 'b': '2'}:
|
||||
print(i)
|
||||
|
||||
a = iter('111')
|
||||
print(next(a))
|
||||
print(next(a))
|
||||
print(next(a))
|
||||
|
||||
|
||||
class Reverse:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.index = len(data)
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
if self.index == 0:
|
||||
raise StopIteration
|
||||
self.index = self.index - 1
|
||||
return self.data[self.index]
|
||||
|
||||
|
||||
rev = Reverse('spam')
|
||||
print(iter(rev))
|
||||
|
||||
for char in rev:
|
||||
print(char)
|
||||
|
||||
|
||||
def reverse(data):
|
||||
for index in range(len(data) - 1, -1, -1):
|
||||
yield data[index]
|
||||
|
||||
|
||||
for char in reverse('golf'):
|
||||
print(char)
|
||||
Loading…
x
Reference in New Issue
Block a user