>> print type(a) >>> response.content...">

As the contents of the "REST" response are "magically" transformed from a "list" to a "string",

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

Could you explain this β€œmagic” to me? How is a converted from list to string ?

response is an instance of rest_framework.response.Response .

+6
source share
2 answers

I believe this class does this conversion by defining the __setattr__ method. You can read http://docs.python.org/2.7/reference/datamodel.html#customizing-attribute-access for more information.

+2
source

There are only a couple of ways you could have something like this. The most common reason is that response.content is implemented as a kind of handle, such interesting things can happen. (A typical handle that would work this way would be a property object). In this case, the getter property returns a string. As an official example:

 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) 
+8
source

Source: https://habr.com/ru/post/943904/


All Articles