python - 我怎样才能打印这个类的属性;;结果没有按预期返回?
问题描述
我目前正在编写一个类 Book 并尝试打印它的一个属性,但不断返回以下内容而不是 :: <__main__.Person object at 0x1055f5d68>
。
class Book:
def __init__(self, title):
self.title=title
self.person=None
self.waitlist=[]
def __str__(self):
if self.person!=None:
result = str(self.person)
else:
for person in self.waitlist:
result += str(person)
return result
def borrow(self, person):
if self.person==None:
self.person=person
return self.__str__()
#This is where the message is coming.
#Rather than showing the person, it shows:
# <__main__.Person object at 0x1055f5d6>
else:
self.waitlist.append(person)
return self.__str__()
#I would also like to print the people in the waitlist here, but shows the same result.
我也尝试过使用方法打印属性 person __repr__
,__str__
但在这种情况下它不起作用。任何想法将不胜感激!
解决方案
推荐阅读
- c++ - 表达式不能作为函数,c++机器码函数
- sql - 为一系列重叠事件对创建 episodeID
- python - Django sql-修改onclick函数
- c# - 从一个内容页面导航到另一个内容页面时菜单消失
- wordpress - Wordpress:> 显示为 >
- scala - 为什么 Scala By-Name 参数语法在删除 : 和 => 之间的空格时失败
- android - 添加网络检测代码后FAB的空指针异常(即使定义好)
- google-chrome - Chrome 扩展 webRequest 侦听器在 Chrome 启动时不起作用
- android - 无法解析 Google 登录类
- r - 通过查找 r 中 2 个日期之间的差异来聚合列