写法 触发 对应的内置方法
实例化
对象
del 对象
print(对象)
str(对象)
'%s'%对象
repr()
'%r'%对象
len(对象)
对象[canshu]
==
__call__
class Son():
def __init__(self):
pass
def __call__(self, *args, **kwargs):
print('被执行了')
a = Son()
a()
print(callable(a)) #查看某个变量能否被调用 返回的是True 就是能被调用
print(callable(Son)) #查看某个变量能否被调用 返回的是True 就是能被调用
__new__ 构造方法,提供了一个类实例化对象的时候所有需要的内存空间
实例化的过程:创建一个内存空间 执行init,传self,返回self
class Dog():
def __new__(cls, *args, **kwargs):
dog_obj = object.__new__(cls)
return dog_obj
def __init__(self,name,age):
self.name = name
self.age = age
wc = Dog('玩彩',8)
print(wc.name)
单例模式
class Dog:
flag = None
count = 0
def __new__(cls, *args, **kwargs):
if cls.flag is None:
cls.flag = object.__new__(cls)
cls.count += 1
return cls.flag
return cls.flag
def __call__(self, *args, **kwargs):
Dog.count += 1
return Dog.count
a = Dog()
q = Dog()
print(a())
class Teacher:
flag = None
def __new__(cls, *args, **kwargs):
if cls.flag is None:
cls.flag = object.__new__(cls) # 这一句话只能走一次
return cls.flag
def __init__(self,name):
self.name = name
alex1 = Teacher('alex')
alex2 = Teacher('alex')
yuan = Teacher('yuan')
print(alex1.name)
print(yuan.name)
析构方法(了解)删除
执行del对象的时候出发__del__,在真正的删除alex对象之前,执行的方法__del__
如果我们自己不删除alex,name在程序的执行过程中或者最后垃圾回收机制会替你执行del alex
class Teacher:
def __init__(self,name):
self.name = name
def __del__(self):
print('执行我啦')
alex = Teacher('你还')
# del alex #删除
print(alex.name) #删除后执行将会报错
class Course():
def __init__(self,name,price,period,teacher):
self.name = name
self.price = price
self.teacher = teacher
self.period = period
def __str__(self):
return '%10s%10s%20s%10s'% (self.name,self.price,self.period,self.teacher)
lst = []
python = Course('python',19800,'6 months','女神')
linux = Course('linux',19800,'6 months','女神')
print(python)
print(str(python))
print('课程显示: %s'%python)
lst.append(python)
lst.append(linux)
for i in lst:
print(i)
__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):
return 'repr>>>%s%s%s%s'%(self.name,self.price,self.period,self.teacher)
def __str__(self):
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)
流畅的python:repr和str如果只能写一个的 就写 repr
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'])
python['name'] = '你好'
print(python['name'])
del python['teacher']
print(python.teacher)
class Ruler():
def __init__(self,price,length,jingdu):
self.length = length
self.price = price
self.jingdu = jingdu
def __len__(self):
return self.length
ret = Ruler(2.5,14,0.1)
print(len(ret))
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('锚',5)
s2 = Student('锚',5)
print(s1 == s2)
print(s1 is s2)
print(id(s1),id(s2))