考虑这个课程:
class foo(object):
pass
Run Code Online (Sandbox Code Playgroud)
默认字符串表示形式如下所示:
>>> str(foo)
"<class '__main__.foo'>"
Run Code Online (Sandbox Code Playgroud)
如何将此显示设为自定义字符串?
Ign*_*ams 249
实现__str__()
或__repr__()
在类的元类中.
class MC(type):
def __repr__(self):
return 'Wahaha!'
class C(object):
__metaclass__ = MC
print C
Run Code Online (Sandbox Code Playgroud)
使用__str__
,如果你说的是可读的字串,使用__repr__
了明确的表示.
小智 21
class foo(object):
def __str__(self):
return "representation"
def __unicode__(self):
return u"representation"
Run Code Online (Sandbox Code Playgroud)
use*_*754 11
如果您必须在第一个之间进行选择__repr__
或者选择__str__
第一个,则默认情况下在未定义时执行__str__
调用__repr__
.
自定义Vector3示例:
class Vector3(object):
def __init__(self, args):
self.x = args[0]
self.y = args[1]
self.z = args[2]
def __repr__(self):
return "Vector3([{0},{1},{2}])".format(self.x, self.y, self.z)
def __str__(self):
return "x: {0}, y: {1}, z: {2}".format(self.x, self.y, self.z)
Run Code Online (Sandbox Code Playgroud)
在此示例中,repr
再次返回可以直接使用/执行的字符串,而str
作为调试输出更有用.
v = Vector3([1,2,3])
print repr(v) #Vector3([1,2,3])
print str(v) #Vector(x:1, y:2, z:3)
Run Code Online (Sandbox Code Playgroud)
Ignacio Vazquez-Abrams 认可的答案是正确的。然而,它来自 Python 2 代。现在的 Python 3 的更新将是:
class MC(type):
def __repr__(self):
return 'Wahaha!'
class C(object, metaclass=MC):
pass
print(C)
Run Code Online (Sandbox Code Playgroud)
如果您想要在 Python 2 和 Python 3 上运行的代码,六个模块为您提供:
from __future__ import print_function
from six import with_metaclass
class MC(type):
def __repr__(self):
return 'Wahaha!'
class C(with_metaclass(MC)):
pass
print(C)
Run Code Online (Sandbox Code Playgroud)
最后,如果您有一个类想要自定义静态表示,上面的基于类的方法效果很好。但是如果你有几个,你就必须MC
为每个生成一个类似于元类,这可能会让人厌烦。在这种情况下,进一步进行元编程并创建元类工厂会使事情变得更清晰:
from __future__ import print_function
from six import with_metaclass
def custom_class_repr(name):
"""
Factory that returns custom metaclass with a class ``__repr__`` that
returns ``name``.
"""
return type('whatever', (type,), {'__repr__': lambda self: name})
class C(with_metaclass(custom_class_repr('Wahaha!'))): pass
class D(with_metaclass(custom_class_repr('Booyah!'))): pass
class E(with_metaclass(custom_class_repr('Gotcha!'))): pass
print(C, D, E)
Run Code Online (Sandbox Code Playgroud)
印刷:
Wahaha! Booyah! Gotcha!
Run Code Online (Sandbox Code Playgroud)
元编程不是你每天都需要的东西——但是当你需要它时,它真的很到位!