本文介绍了__repr__ 方法的目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def __repr__(self):返回 ''% (self.__class__.__name__, self.urlconf_name, self.app_name,self.namespace, self.regex.pattern)
这种方法的意义/目的是什么?
解决方案
__repr__
应该返回对象的可打印表示,很可能是创建方法中的一种这个对象.请参阅官方文档此处.__repr__
更适用于开发人员,而 __str__
更适用于最终用户.
一个简单的例子:
>>>类点:... def __init__(self, x, y):... self.x, self.y = x, y... def __repr__(self):... return 'Point(x=%s, y=%s)' % (self.x, self.y)>>>p = 点(1, 2)>>>磷点(x=1,y=2)def __repr__(self):
return '<%s %s (%s:%s) %s>' % (
self.__class__.__name__, self.urlconf_name, self.app_name,
self.namespace, self.regex.pattern)
What is the significance/purpose of this method?
解决方案
__repr__
should return a printable representation of the object, most likely one of the ways possible to create this object. See official documentation here. __repr__
is more for developers while __str__
is for end users.
A simple example:
>>> class Point:
... def __init__(self, x, y):
... self.x, self.y = x, y
... def __repr__(self):
... return 'Point(x=%s, y=%s)' % (self.x, self.y)
>>> p = Point(1, 2)
>>> p
Point(x=1, y=2)
这篇关于__repr__ 方法的目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!