Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何打印存储在数组中的属性而不是对象?_Python_Arrays_Object_Printing_Instance - Fatal编程技术网

Python 如何打印存储在数组中的属性而不是对象?

Python 如何打印存储在数组中的属性而不是对象?,python,arrays,object,printing,instance,Python,Arrays,Object,Printing,Instance,如何打印存储在数组中的chaptername属性而不是它的对象 class Book: def __init__(self): self._chapters = [] def addchapter(self, chapter): self._chapters.append(chapter) def getchapters(self): #How do I print out ["First", &qu

如何打印存储在数组中的chaptername属性而不是它的对象

class Book:
    def __init__(self):
        self._chapters = []

    def addchapter(self, chapter):
        self._chapters.append(chapter)

    def getchapters(self):
        #How do I print out ["First", "Second", "Third"] instead?
        print(self._chapters) #<- prints out [<__main__.Chapter object at 0x10b849c40>, <__main__.Chapter object at 0x10b8901f0>, <__main__.Chapter object at 0x10b890310>]


class Chapter:
    def __init__(self, chaptername):
        self._chaptername = chaptername

    def __str__(self):
        return f"{self._chaptername}"


b = Book()
b.addchapter( Chapter("First") )
b.addchapter( Chapter("Second") )
b.addchapter( Chapter("Third") )
b.getchapters() 
课程手册:
定义初始化(自):
self._章=[]
def添加章节(自我,章节):
self.\u章。附加(章)
def getchapters(自我):
#如何打印[“第一”、“第二”、“第三”]?

打印(self._chapters)#你想覆盖
\uuu repr\uuu
而不是
\uu str\uu

哦,我的天啊!你是救世主!非常感谢,我可以像这样访问数组吗,因为它不是数组?打印(self.\uu章[1])@Shee-yep试试看你太棒了。非常感谢。