python - REST 响应内容 "magically"如何从 'list' 转换为 'string'

标签 python rest

>>> print type(a)
<type 'list'>
>>> response.content = a
>>> print type(response.content)
<type 'str'>

你能给我解释一下这个“魔法”吗? a如何从list转换为string

responserest_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/

相关文章:

c++ - 将 Python 算法移植到 C++ - 不同的解决方案

python - 匹配数字的正则表达式(正则和罗马)

python - OpenCV 3.4.1 : KNearest function not found

spring - 带有 Spring RequestMapping 路径参数的编码斜杠 (%2F) 提供 HTTP 400

java - java spring请求体中的转义引号

python - PyTorch 中是否有提取图像补丁的功能?

python - 哪个是在 Django 中查询的更好方法(原始查询与 ORM)?

asp.net - 是否可以使用 OAuth2 来保护 "non-rest"应用程序

java - 在 spring rest Api 中启用 SSL (HTTPS)

xml - RESTful VS XML 样式的请求