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))


分享到:


相關文章: