首页 > 解决方案 > 我怎样才能打印这个类的属性;;结果没有按预期返回?

问题描述

我目前正在编写一个类 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__但在这种情况下它不起作用。任何想法将不胜感激!

标签: pythonclass

解决方案


推荐阅读