__init__函数一定需要吗

其中,init 表示构造函数,意即一个对象生成时会被自动调用的函数



没有init构造函数呢?

class Document():
    # def __init__(self):
    #     pass
    def fun1(self,a):
        return a
harry_potter_book = Document()
print type(harry_potter_book)
print dir(harry_potter_book)
print harry_potter_book.fun1('xxnn')

C:Python27python.exe "C:/Users/TLCB/PycharmProjects/untitled2/python study/t12.py"
<type 'instance'>
['__doc__', '__module__', 'fun1']
xxnn

但是你需要实例化参数呢?那么就必须要用__init__函数了
原文地址:https://www.cnblogs.com/hzcya1995/p/13348371.html