class Cat: def __init__(self,_name): self.name = _name def eat(self): print("i am eating .") def show(self): print("name is %s"%self.name) tom = Cat("tom") tom.show() print("------------"); lanmao = Cat("lanpang") lanmao.show()
python類的創建過程 1.分配內存空間 2.調用__init__方法,並且傳遞參數 3.將地址返回給變量
__init__函數的使用,使得python中的類更加接近於c++中的類,通過在__init__函數中定義變量達到在類中統一定義屬性的目的