在实例上调用repr()
函数:Book
object.__repr__(self)
[文档]
Called by the repr() built-in function and by string conversions (reverse quotes)
to compute the “official” string representation of an object. [...] The return
value must be a string object. If a class defines __repr__() but not __str__(),
then __repr__() is also used when an “informal” string representation of
instances of that class is required.
class Book(object):
def __repr__(self):
return 'I am a book'
class Library(object):
def __init__(self,*books):
self.books = books
def __repr__(self):
return ' | '.join(repr(book) for book in self.books)
b1, b2 = Book(), Book()
print Library(b1,b2)
#prints I am a book | I am a book