如何在python中打印/返回一个类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12802016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to print/return a class in python?
提问by iamtesla
Possible Duplicate:
Python: How to print a class or objects of class using print()?
I currently have this code:
我目前有这个代码:
class Track(object):
def __init__(self,artist,title,album=None):
self.artist = artist
self.title = title
self.album = album
def __str__(self):
return self.title + self.artist + self.album
Now when I put something like Track('Kanye West','Roses','Late Registration')into the terminal I get <__main__.Track object at 0x10f3e0c50>How can I get it to return or print the value at that place?
现在,当我将类似的东西Track('Kanye West','Roses','Late Registration')放入终端时,我得到<__main__.Track object at 0x10f3e0c50>如何让它在那个地方返回或打印值?
I'm new to programming and especially new to 'object oriented programming', so my question is what exactly is a class? How do I define a function within a class?
我是编程新手,尤其是“面向对象编程”的新手,所以我的问题是类到底是什么?如何在类中定义函数?
回答by defuz
You should define __repr__method:
您应该定义__repr__方法:
class Track(object):
...
def __repr__(self):
return ('Track(artist=%s album=%s title=%s)'
% (repr(self.artist), repr(self.title), repr(self.album)))
Now you can see representation of object in terminal:
现在您可以在终端中看到对象的表示:
>>> Track('Kanye West','Roses','Late Registration')
Track(artist='Kanye West' album='Roses' title='Late Registration')
Notethat there is a difference between __str__and __repr__method:
请注意,__str__和__repr__方法之间存在差异:
__str__needs to convert an object into a string. Calling:str(obj)__repr__- for its human readable visualization. Calling:repr(obj)
__str__需要将对象转换为字符串。调用:str(obj)__repr__- 因为其人类可读的可视化。调用:repr(obj)
When you put into the terminal obj, python calls reprfunction automatically. But if you use print objexpression, python calls strfunction:
当你进入终端时obj,python 会repr自动调用函数。但是如果使用print obj表达式,python 调用str函数:
>>> obj
...repr of obj here...
>>> print obj
...str of obj here...
See docfor more information.
有关更多信息,请参阅文档。
回答by Inbar Rose
Your code is fine, but you are forgetting something.
你的代码很好,但你忘记了一些东西。
Lets say you have this class:
假设你有这个类:
class Bob():
def __init__(self, hobby):
self.hobby = hobby
In the console you would make a Bob class with a hobby like this:
在控制台中,您将创建一个具有以下爱好的 Bob 课程:
a = Bob("skiing")
then to get Bob's hobby you can do:
然后为了得到鲍勃的爱好,你可以这样做:
print a.hobby
now - back to your problem, you did Track('Kanye West','Roses','Late Registration')
现在 - 回到你的问题,你做到了 Track('Kanye West','Roses','Late Registration')
you created an object, but you did not assign the object to a variable. so your result is the object itself... you could simply print the object, which will invoke the __str__method. so...
您创建了一个对象,但没有将该对象分配给变量。所以你的结果是对象本身......你可以简单地打印对象,这将调用该__str__方法。所以...
print Track('Kanye West','Roses','Late Registration')
would work, but if you wanted to do it a bit nicer. (for example)
会工作,但如果你想做得更好一点。(例如)
a = Track('Kanye West','Roses','Late Registration')
print a.title
print a.artist
print a.album
回答by yakxxx
If you need this just for debuging purpose to inspect internal values of your object you can do it like this
如果您只是为了调试目的而需要它来检查对象的内部值,您可以这样做
Track('Kanye West','Roses','Late Registration').__dict__
It will show you internal data representation as a python dict
它将向您显示内部数据表示为 python dict

