标签:函数返回 together 字符串表 pytho util name nbsp 对象 close
英文文档:
repr
(object)eval()
, otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__()
method.
>>> a = ‘some text‘ >>> str(a) ‘some text‘ >>> repr(a) "‘some text‘"
2. repr函数的结果一般能通过eval()求值的方法获取到原对象。
>>> eval(repr(a)) ‘some text‘
3. 对于一般的类型,对其实例调用repr函数返回的是其所属的类型和被定义的模块,以及内存地址组成的字符串。
>>> class Student: def __init__(self,name): self.name = name >>> a = Student(‘Bob‘) >>> repr(a) ‘<__main__.Student object at 0x037C4EB0>‘
4. 如果要改变类型的repr函数显示信息,需要在类型中定义__repr__函数进行控制。
>>> class Student: def __init__(self,name): self.name = name def __repr__(self): return (‘a student named ‘ + self.name) >>> b = Student(‘Kim‘) >>> repr(b) ‘a student named Kim‘
标签:函数返回 together 字符串表 pytho util name nbsp 对象 close
原文地址:http://www.cnblogs.com/sesshoumaru/p/6060307.html