跳转至

34

本文总阅读量次 ,原创教程,严禁转载
@author jackzhenguo
@desc 
@date 2019/3/10

34 ascii 展示对象

调用对象的 __repr__ 方法,获得该方法的返回值,如下例子返回值为字符串

>>> class Student():
    def __init__(self,id,name):
        self.id = id
        self.name = name
    def __repr__(self):
        return 'id = '+self.id +', name = '+self.name
调用:
>>> xiaoming = Student(id='1',name='xiaoming')
>>> xiaoming
id = 1, name = xiaoming
>>> ascii(xiaoming)
'id = 1, name = xiaoming'

上一个例子 下一个例子