concept _ _ init _ _ in category python

This is an excerpt from Manning's book Python Workout: 50 ten-minute exercises.
Table 9.1 What you need to know (view table figure)
Method that returns a string containing an object’s printed representation
Returns a proxy object on which methods can be invoked; typically used to invoke a method on a parent class
It creates a new instance of
Person
and holds onto it as a local variable. But then__new__
calls__init__
. It passes the newly created object as the first argument to__init__
, then it passes all additional arguments using*args
and**kwargs
, as shown in figure 9.4.
class LoudIterator(): def __init__(self, data): print('\tNow in __init__') self.data = data #1 self.index = 0 #2 def __iter__(self): print('\tNow in __iter__') return self #3 def __next__(self): print('\tNow in __next__') if self.index >= len(self.data): #4 print( f'\tself.index ({self.index}) is too big; exiting') raise StopIteration value = self.data[self.index] #5 self.index += 1 #6 print('\tGot value {value}, incremented index to {self.index}') return value for one_item in LoudIterator('abc'): print(one_item)

This is an excerpt from Manning's book Get Programming: Learn to code with Python.
To initialize your object, you have to implement a special operation, the __init__ operation (notice the double underscores before and after the word init):
class Circle(object): def __init__(self): # code hereThe __init__ definition looks like a function, except it’s defined inside a class. Any function defined inside a class is named a method.