几个内置类方法

__str__和__repr__

改变对象的字符串显示__str__,__repr__

自定制格式化字符串__format__

我们以前很熟悉len(),但是在执行len()的时候,代码内部实际上调用的就是__len__()

内置的类方法和内置的函数之间存在这千丝万缕的联系

obj.__str__等价于str(),现在我们已知,str(1),就是传递一个int对象
obj.__repr__等价于repr(),现在我们已知,

repr函数复习

print(1)
print('1')
print(repr('1'))

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/145.py
1
1
'1'

Process finished with exit code 0

 

__str__

class A:pass
    # def __str__(self):
    #     return 'A'
a=A()
print(str(a))#因为这里首先会在a的方法里面找__str__()方法,但是可惜没找到,然后它就会其父类中找__str__,找到了所以打印出来的结果就是一个内存地址。所以实际上执行的是object里面__str__方法的内存地址
#object里面有一个__str__(self),一旦被调用,就返回调用这个方法对象的内存地址


D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
<__main__.A object at 0x000001494F726048>

Process finished with exit code 0

修改

class A:
    pass#注意这里的这个pass必须写在这里
    def __str__(self):
        return 'A'
 
a=A()
print(str(a))#此时a里面有__str__方法,所以得到结果就是'A'
print(a)#这里和上面的代码作用类似,相当于调用了a. __str__

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
A
A

Process finished with exit code 0

解释

class A:
    pass#注意这里的这个pass必须写在这里
    def __str__(self):
        return 'A'
    def func(self):
        return 'wahah'
a=A()
print(str(a))
print(a)#即使类中增加了一个func函数,打印出来的结果依然是"A",这就是这个方法的妙处

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
A
A

Process finished with exit code 0

注意对比

class A:
    pass#注意这里的这个pass必须写在这里
    def __str__(self):
        return 'A'
    def func(self):
        return 'wahah'
a=A()

print('%s:%s'%('A',a))

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
A:A

Process finished with exit code 0

删掉__str__

class A:
    pass#注意这里的这个pass必须写在这里
    # def __str__(self):
    #     return 'A'
    def func(self):
        return 'wahah'
a=A()
print('%s:%s'%('A',a))

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
A:<__main__.A object at 0x0000024442575A90>

Process finished with exit code 0

举一个实际例子

class Teacher:
    def __init__(self,name,salary):
        self.name=name
        self.salary=salary
    def __str__(self):
        return 'Teacher object;%s'%self.name

Jack=Teacher('tom',236)
print(Jack)

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
Teacher object;tom

Process finished with exit code 0

引入__repr__

 

class Teacher:
    def __init__(self,name,salary):
        self.name=name
        self.salary=salary
    def __str__(self):
        return 'Teacher object;%s'%self.name
    def __repr__(self):
        return str(self.__dict__)

Jack=Teacher('tom',236)
print(Jack)
print(repr(Jack))
print('%r'%Jack)#%r相当于str中的%s

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
Teacher object;tom
{'name': 'tom', 'salary': 236}
{'name': 'tom', 'salary': 236}

Process finished with exit code 0

repr是str的备胎,但是str不能做repr的备胎
print(obj)/'%s'%obj/str(obj)的时候,实际上是内部调用了obj.__str__方法,如果str方法有,那么它返回的必须是一个字符串,如果没有__str__方法,会先找本类中的__repr__方法,再没有就去父类object中找。
与此相对,repr()只会找__repr__方法,如果没有就去找父类。
所以如果按照要求__str__方法和__repr__方法只能选一种,那么就选__repr__方法,因为可以给__str__方法使用

内置的方法有很多,不一定全部都在object中

__del__

class A:
    def __del__(self):
        print('删除')
a=A()
del a#这里既删除了变量,又执行了方法
print(a)

在pycharm中如果一个变量下面不再使用,那么在一定时间后将会被删除

class A:
    def __del__(self):
        print('删除')
a=A()#这个实例化a后面不再使用,大约2秒后被删除,执行del方法

进阶

class A:
    def __del__(self):
        self.f.close
a=A()
a.f=open()#打开一个文件,出现在内存中
del a#得到这个文件操作符,消失在内存中

__call__

class A:
    def __init__(self,name):
        self.name=name
    def __call__(self):
        print('执行我了')
        for k in self.__dict__:
            print(k,self.__dict__[k])
a=A('123')
a()#一个对象加上括号,就表示执行了call方法

D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/3.二分查找算法.py
执行我了
name 123

Process finished with exit code 0

 

posted @ 2019-03-14 21:06  舒畅123  阅读(117)  评论(0编辑  收藏  举报