Test iteration.
This commit is contained in:
parent
e699ebf017
commit
8fc8c48ff3
51
main.py
51
main.py
@ -1,31 +1,24 @@
|
|||||||
#Train Class Example
|
#Iteration test
|
||||||
class Train:
|
a = (1, 2, 3)
|
||||||
def __init__(self, class_, name):
|
b = iter(a)
|
||||||
self.class_ = class_
|
print(next(b))
|
||||||
self.name = name
|
print(next(b))
|
||||||
self.name = name if name else "#"
|
print(next(b))
|
||||||
def __str__(self):
|
|
||||||
return f"Class {self.class_}({self.name})"
|
|
||||||
def manufacturer(self):
|
|
||||||
return ""
|
|
||||||
|
|
||||||
class BREL(Train):
|
class A:
|
||||||
def __init__(self, class_):
|
|
||||||
super().__init__(class_, "")
|
|
||||||
def manufacturer(self):
|
|
||||||
return "BREL"
|
|
||||||
|
|
||||||
class PEP(Train):
|
|
||||||
def __init__(self, class_):
|
|
||||||
super().__init__(class_, "PEP")
|
|
||||||
def manufacturer(self):
|
|
||||||
return "Unknown"
|
|
||||||
|
|
||||||
#The presendence of definitions is the order in the inheritance bracket
|
|
||||||
class C315(BREL, PEP):
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(BREL,self).__init__(315)
|
self.n = 0
|
||||||
|
def __init__(self, n):
|
||||||
c315 = C315()
|
self.n = n
|
||||||
print(c315, c315.manufacturer())
|
def __iter__(self):
|
||||||
del c315
|
self.i = 0
|
||||||
|
return self
|
||||||
|
def __next__(self):
|
||||||
|
if self.i > 100 or self.i < -100: raise StopIteration
|
||||||
|
z = self.i
|
||||||
|
self.i -= self.n
|
||||||
|
return z
|
||||||
|
c = A(2)
|
||||||
|
d = iter(c)
|
||||||
|
for x in d:
|
||||||
|
print(x)
|
||||||
|
Loading…
Reference in New Issue
Block a user