Python的字符串和unicode强制/魔术函数如何工作?

Sna*_*fee 5 python coercion python-2.7

我使用的是Python版本:2.7.3.

在Python中,我们使用了魔术方法__str____unicode__定义的行为strunicode我们的自定义类:

>>> class A(object):
  def __str__(self):
    print 'Casting A to str'
    return u'String'
  def __unicode__(self):
    print 'Casting A to unicode'
    return 'Unicode'


>>> a = A()
>>> str(a)
Casting A to str
'String'
>>> unicode(a)
Casting A to unicode
u'Unicode'
Run Code Online (Sandbox Code Playgroud)

该行为表明来自__str____unicode__的强制返回值被强制为str或者unicode取决于运行哪种魔术方法.

但是,如果我们这样做:

>>> class B(object):
  def __str__(self):
    print 'Casting B to str'
    return A()
  def __unicode__(self):
    print 'Casting B to unicode'
    return A()


>>> b = B()
>>> str(b)
Casting B to str

Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    str(b)
TypeError: __str__ returned non-string (type A)
>>> unicode(b)
Casting B to unicode

Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    unicode(b)
TypeError: coercing to Unicode: need string or buffer, A found
Run Code Online (Sandbox Code Playgroud)

调用str.mro()unicode.mro()说两者都是子类basestring.但是,__unicode__也允许返回buffer直接继承object和不继承的对象basestring.

所以,我的问题是,到底发生了什么时,strunicode被称为?什么是返回值的要求上__str__,并__unicode__在使用strunicode

Bur*_*lid 4

但是,__unicode__也允许返回缓冲区对象,该对象直接对象并且不继承自basestring。

这是不正确的。unicode()可以转换字符串或缓冲区。这是使用默认编码将传递的参数转换为 unicode 的“最佳尝试”(这就是为什么它说coercing)。它将始终返回一个 unicode 对象。

所以,我的问题是,调用 str 和 unicode 时实际上会发生什么?__str__str和 unicode 中使用的返回值要求是什么__unicode__

__str__应该返回对象的非正式的、人类友好的字符串表示形式。str()当有人在您的对象上使用或当您的对象是打印语句的一部分时,这就是所谓的。

__unicode__应该总是返回一个unicode对象。如果未定义此方法,__str__则调用该方法,然后将结果强制为 unicode(通过将它们传递给unicode())。

在第二个示例中,您返回无效对象,这就是您看到错误消息的原因。__unicode__由于副作用,您的第一个示例似乎有效,但它也没有正确编写。

文档的数据模型部分值得一读,以获取有关这些“神奇方法”的更多信息和细节。