【问题标题】:Python: why __str__ not call recursive?Python:为什么 __str__ 不调用递归?
【发布时间】:2013-11-25 02:14:38
【问题描述】:

将 dict 转换为 JSON 对象时遇到一些麻烦。我有这门课:

class ServerResponse(object):

    status = None
    code = None
    message = None
    data = None

    OK_STATUS = True
    ERROR_STATUS = False

    OK_CODE = 200

    def __init__(self, status=OK_STATUS, code=OK_CODE, message=None, data=None, *args, **kwargs):
        self.status = status
        self.code = code
        self.message = message
        self.data = data

    def to_dict(self):
        fields = {
            "status": self.status,
            "code": self.code,
            "message": self.message,
            "data": str(self.data),
        }

        return fields

    def to_json(self):
        return json.dumps(self.to_dict())

    def __str__(self):
        return self.to_json()

我使用这个类来生成服务器答案。

from server_response import ServerResponse as Response
...
return_data = {}

for (name, content) in result.items():
    if not previous_hashes or client.is_data_change(previous_hashes[name], data['hash']):
        return_data[name] = Response(data=content)
    else:
        return_data[name] = Response(code=201, message="Data has not changed")

response = Response(data=return_data)
...
self.write(str(response))

在服务器的回答中,我得到了下一个 JSON

{u'status': True, u'message': None, u'code': 200, u'data': u"{'client': <server_response.ServerResponse object at 0x14e9710>, 'service': <server_response.ServerResponse object at 0x14e90d0>}"}

为什么__str__函数不递归调用?

【问题讨论】:

  • content 看起来像一个普通的 dict,而不是 ServerResponse 对象
  • 是的,但在return_data 中转换不正确。 return_data 是一个 dict,其对象为 ServerResponse。为什么在 dict 上调用 __str__ 而不是在 ServerResponse 上调用 __str__

标签: python string type-conversion


【解决方案1】:

来自这个程序:

class Foo(object):
    def __repr__(self):
        return "REPR"

    def __str__(self):
        return "STR"

x = {}

x['client'] = Foo()

print str(x)
print repr(x)

您可以看到,dict 总是在其成员上调用 repr,无论在 dict 上使用的是 str 还是 repr。

【讨论】:

  • 这解释了为什么__str__ 不调用dict 成员!谢谢!
【解决方案2】:
"data": str(self.data),

字符串化 dict 使用每个键和值的 repr,而不是 str()。

def to_dict(self):
  fields = {
    "status": self.status,
    "code": self.code,
    "message": self.message,
    }
  if self.data is None:
    fields["data"] = None
  else:
    fields["data"] = self.data.to_dict()
  return fields

def to_json(self):
  return json.dumps(self.to_dict())

【讨论】: