__init__()
__init__() 是类的内置方法,用于初始化类的内部状态,当我们去实例化一个对象的时候,默认就会执行 __init__() 下面的语句,例子如下:
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): def __init__(self): self.name = 'Tom' self.age = 23 self.talk() def talk(self): print('My name is %s' % self.name) print('My age is %s' % self.age) if __name__ == '__main__': obj = People()
[root@localhost ~]$ python 1.py My name is Tom My age is 23