OOP - papatwo/PythonNoob GitHub Wiki

类class

Usually, we don’t think much about the creation and destruction of variables, but often as our objects become more complex, we need to take some action within the object to set things up as the object is constructed and possibly clean things up as the object is discarded.

随着对象的复杂程度增加,我们要考虑在创建class的时候让对象意识到被construct和discard。

class PartyAnimal:
	def __init__(self): # 属于该类的obj被创建
		pass

	def __del__(self): # 当该obj被assign新value的时候自动call discard method
		pass

__init__也可以接受用于initialise的parameter

class PartyAnimal:
	def __init__(self, nam):
		self.name = nam

	def __del__(self):
		pass

s.PartyAnimal('sally') # 在main里创建该类的obj时输入的parameter都会传入__init__作为initialization的参数