0
How can we use "return self"?
Where can we use "return self"? There is the example of working code with it: class A: def __init__(self): return None def add(self): return self onj=A() print(onj.add)
1 Resposta
0
'return self' returns the current object itself. We do not want to use it most of the time, but it can be useful when you want to make your own iterator.
e.g.
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
x = self.a
self.a += 1
return x