>>> print type(a)
<type 'list'>
>>> response.content = a
>>> print type(response.content)
<type 'str'>
你能给我解释一下这个“魔法”吗? a
如何从list
转换为string
?
response
是 rest_framework.response.Response
的实例。
最佳答案
只有几种方法可以让这样的事情发生。最常见的原因是,如果 response.content
被实现为某种描述符,可能会发生这样有趣的事情。 (像这样操作的典型描述符是一个 property
对象)。在这种情况下,属性的 getter 将返回一个字符串。作为一个正式的例子:
class Foo(self):
def __init__(self):
self._x = 1
@property
def attribute_like(self):
return str(self._x)
@attribute_like.setter
def attribute_like(self,value):
self._x = value
f = Foo()
f.attribute_like = [1,2,3]
print type(f.attribute_like)
关于python - REST 响应内容 "magically"如何从 'list' 转换为 'string',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286363/