Python學習筆記——定義類

1、定義類

class className:

2、構造函數

def __init__(self,other):

3、析構函數

def __del__():

4、新式類(python3所有的類都是新式類)

class NewStyle(object):

pass

5、兩個內建函數

dir()返回一個對象屬性

type()獲取對象類型

6、例子

class OldStyle:def __init__(self,name,description):self.name = nameself.description = descriptionclass NewStyle(object):def __init__(self,name,description):self.name = nameself.description = descriptionif __name__ =='__main__':old = OldStyle('old', 'old style class')print(old)print(type(old))print(dir(old))print('-------------------------------------------')new = NewStyle('new','new style class')print(new)print (type(new))print (dir(new))


分享到:


相關文章: