Shallowly discuss the function of method 1en__ in class Python and the constructor of class Python

  • 2020-05-17 05:54:42
  • OfStack

If there is no method function in a class, the instance object created by the class name is empty and not initialized. If you have this method function, it's usually used as the first method function of a class, kind of like a constructor in languages like C++.


class Ca:
def __init__(self, v): #  Notice the two underscores before and after 
self.name = v
def pr(self):
print "a--->", self.name
ia = Ca("Jeapedu") #  The essence calls __init__ Methods the function 
ia.pr()
Ca.pr(ia) 

The output
a--- > Jeapedu

a--- > Jeapedu


Related articles: