Python之路——类内置方法
1 # __str__ __repr__ 2 # %s str() %r repr() 3 # class A: 4 # def __init__(self,name): 5 # self.name = name 6 # def __str__(self): 7 # return self.name 8 # # def __repr__(self): 9 # # return self.name 10 # a = A('alex') 11 # str(a)---->a.__str__() 12 # repr(a)----> a.__repr() 13 # print(str(a)) # alex 14 # print(repr(a)) # <__main__.A object at 0x00000232AD2F9710> 15 16 # object 里有一个__str__,一旦被调用,就返回调用这个方法的对象的内存地址 17 # repr是str的备胎,但str不能做repr的备胎 18 # print(obj)/'%s'%obj/str(obj)的时候,实际上是内部调用了obj.__str__方法,如果str方法有,那么他返回的必定是一个字符串 19 # 如果没有__str__方法,会先找本类中的__repr__方法,再没有再找父类中的__str__。 20 # repr(),只会找__repr__,如果没有找父类的 21 22 # 内置的方法有很多 23 # 不一定全都在object中 24 25 # __len__ __call__ 26 # class A: 27 # def __len__(self): 28 # return 10 29 # def __call__(self, *args, **kwargs): 30 # print('in the call') 31 # 32 # a = A() 33 # print(len(a)) 34 # a() # 对象()的方式相当于调用了对象的__call__方法 35 36 # __del__ 37 # class A: 38 # def __del__(self): # 析构函数在删除对象前进行一些收尾工作 39 # self.f.close()
__new__
1 # __new__ 2 # class A: 3 # def __init__(self): 4 # self.x = 1 5 # print('in init function') 6 # def __new__(cls, *args, **kwargs): 7 # print('in new function') 8 # return object.__new__(cls) 9 # a = A() 10 # print(a.x) 11 # # 先执行new方法,new创建了一个self对象传给了init方法 12 # # 输出: 13 # # in new function 14 # # in init function 15 # # 1
__eq__和__hash__
1 # __eq__ __hash__ 2 # class A: 3 # def __init__(self): 4 # self.a = 1 5 # self.b = 2 6 # def __eq__(self, other): 7 # if self.a == other.a and self.b == other.b: 8 # return True 9 # a = A() 10 # b = A() 11 # print(a==b) 12 # print(a is b) 13 14 15 # class Person: 16 # def __init__(self,name,age,sex): 17 # self.name = name 18 # self.age = age 19 # self.sex = sex 20 # def __str__(self): 21 # return str([self.name,self.age,self.sex]) 22 # 23 # # 如果没有实现hash方法会报错:TypeError: unhashable type: 'Person' 24 # def __hash__(self): 25 # print('in hash') 26 # return hash(self.name+self.sex) 27 # 28 # def __eq__(self, other): 29 # print('in eq') 30 # if self.name == other.name and self.sex == other.sex:return True 31 # 32 # p_lst = [] 33 # for i in range(10): 34 # p_lst.append(Person('egon',i,'male')) 35 # for i in p_lst: 36 # print(i,end=' ') 37 # # set 去重,需要实现__hash__ 和 __eq__ 方法,先执行hash,后执行eq 38 # p_lst2 = list(set(p_lst)) 39 # print() 40 # for i in p_lst2: 41 # print(i,end=' ')
关于set去重
# 用set对列表去重并保持原来顺序 # mailto = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] # addr_to = list(set(mailto)) # print(addr_to) # a = sorted(addr_to,key=mailto.index) # print(a) # # addr_to.sort(key = mailto.index) # print(addr_to)
__getitem__ __setitem__ __delitem__
1 # class Foo: 2 # def __init__(self,name,age,sex): 3 # self.name = name 4 # self.age = age 5 # self.sex = sex 6 # 7 # def __getitem__(self, item): 8 # if hasattr(self,item): 9 # return self.__dict__[item] 10 # 11 # def __setitem__(self, key, value): 12 # self.__dict__[key] = value 13 # 14 # def __delitem__(self, key): 15 # del self.__dict__[key] 16 # 17 # f = Foo('egon',38,'男') 18 # # print(f['name']) 19 # f['hobby'] = '男' 20 # print(f.hobby) 21 # # print(f.hobby,f['hobby']) 22 # # del f['hobby'] 23 # # del f.hobby # object 原生支持 __delattr__ 24 # # del f['hobby'] # 通过自己实现的 25 # # print(f.__dict__)