内置方法的特点:一定有某一个语法或者一种写法会自动触发这个方法
__init__ :实例化
__init__是初始化方法
class Student(): def __init__(self,name,age): self.name = name self.age = age wl = Student('wanglan',18) print(wl.name) print(wl.age) 结果: wanglan 18
__new__
实例化的过程:创建一个内存空间,执行init,传入self,返回self,内存空间就是__new__创建的,__new__shi构造方法
class Student(object): def __init__(self,name,age): self.name = name self.age = age wl = Student('wanglan',18) print(wl.name) print(wl.age) print(wl.__dict__) #结果: wanglan 18 {'name': 'wanglan', 'age': 18} #程序正常执行了 #加上__new__方法 class Student(object): def __new__(cls, *args, **kwargs): print('我执行了') def __init__(self,name,age): self.name = name self.age = age wl = Student('wanglan',18) print(wl.name) print(wl.age) print(wl.__dict__) 结果: 我执行了 Traceback (most recent call last): print(wl.name) AttributeError: 'NoneType' object has no attribute 'name' Process finished with exit code 1 #报错啦 #调用方法 class Student: def __new__(cls, *args, **kwargs): ret = object.__new__(cls) print('ret',ret) return ret def __init__(self,name,age): self.name = name self.age = age wl = Student('wanglan',18) print('wl',wl) print(wl.name) print(wl.age) 结果: ret <__main__.Student object at 0x000000000290AB70> wl <__main__.Student object at 0x000000000290AB70> wanglan 18
class Student(object): flag = None def __new__(cls, *args, **kwargs): if cls.flag is None: cls.flag =object.__new__(cls) #这一句自走一次 return cls.flag def __init__(self,name,age): self.name = name self.age = age wl = Student('wanglan',18) xm = Student('xiaoming',18) print(wl) print(xm) print(wl.name) print(xm.name) 结果: <__main__.Student object at 0x00000000028DAB70> #地址一样 <__main__.Student object at 0x00000000028DAB70> xiaoming #只开辟了一块空间,name的值被覆盖了,值为最后的name xiaoming
__call__
对象()会触发__call__方法
class Student(): def __init__(self,name,age): self.name = name self.age = age wl = Student('wanglan','18') print(callable(Student)) print(callable(wl)) # wl不能被调用 结果: True False # 加入__call__方法 class Student(): def __init__(self,name,age): self.name = name self.age = age def __call__(self, *args, **kwargs): print('我被调用了') wl = Student('wanglan','18') print(callable(Student)) print(callable(wl)) # wl可以被调用了 wl() #调用 结果: True True 我被调用了
__del__:析构
class File(object): def __init__(self,file_name): self.f = open('file_name') #打开一个文件 def read(self): self.f.read(1024) def __del__(self): # 对象使用的一些操作系统的资源的归还工作/收尾工作 self.f.close() my_f = File() my_f.read()
__str__和__repr__
class Course: def __init__(self,name,price,period,teacher): self.name = name self.price = price self.period = period self.teacher = teacher def __str__(self): # 必须有返回值,必须返回一个str类型 return '%s,%s,%s,%s'%(self.name,self.price,self.period,self.teacher) python = Course('python',19800,'6 months','hehe') linux = Course('linux',16800,'5 months','haha') print(python) #使用__str__方法,打印对象时不会在打印地址,打印的是值 print(linux) print('课程展示 : %s'%python)
__repr__ 是str方法的备胎 (有str调用str,没有str走repr)
class Course: def __init__(self,name,price,period,teacher): self.name = name self.price = price self.period = period self.teacher = teacher def __repr__(self): # 必须有返回值,必须返回一个str类型 return 'repr --> : %s,%s,%s,%s'%(self.name,self.price,self.period,self.teacher) def __str__(self): # 必须有返回值,必须返回一个str类型 return 'str --> : %s,%s,%s,%s'%(self.name,self.price,self.period,self.teacher) python = Course('python',19800,'6 months','baoyuan') linux = Course('linux',16800,'5 months','oldboy') print(python) print(linux) class Course: def __init__(self,name,price,period,teacher): self.name = name self.price = price self.period = period self.teacher = teacher def __repr__(self): # 必须有返回值,必须返回一个str类型 return 'repr --> : %s,%s,%s,%s'%(self.name,self.price,self.period,self.teacher) def __str__(self): # 必须有返回值,必须返回一个str类型 return 'str --> : %s,%s,%s,%s'%(self.name,self.price,self.period,self.teacher) python = Course('python',19800,'6 months','baoyuan') linux = Course('linux',16800,'5 months','oldboy') print('%r'%python) print('%s'%python) print(str(python)) print(repr(python))
item系列
class Course: def __init__(self,name,price,period,teacher): self.name = name self.price = price self.period = period self.teacher = teacher def __len__(self): return len(self.__dict__) def __getitem__(self,item): return self.__dict__[item] def __setitem__(self, key, value): self.__dict__[key] = value def __delitem__(self, key): self.__dict__.pop(key) python = Course('python',19800,'6 months','baoyuan') print(python.name) print(python['name']) # ==> 调用getitem print(python['price']) # ==> 调用getitem python['name'] = 'python2.0' print(python.name) del python.name del python['name'] print(python.__dict__)
__len__
class A: def __init__(self): self.a = 1 self.b = 2 def __len__(self): return len(self.__dict__) a = A() print(len(a))
__eq__
class Student(): def __init__(self,name,age): self.name = name self.age = age def __eq__(self, other): if self.name == other.name and self.age == other.age: return True return False s1 = Student('WL',20) s2 = Student('WL',20) print(s1 == s2) # s1.__eq__(s2) print(s1 is s2) # s1.__eq__(s2)