>>> print type(a)
<type 'list'>
>>> response.content = a
>>> print type(response.content)
<type 'str'>
Could you explain me this "magic?" How is a
converted from list
to string
?
response
is an instance of rest_framework.response.Response
.
There are only a couple ways that you could have something like this happen. The most common reason is if response.content
is implemented as some sort of descriptor, interesting things like this could happen. (The typical descriptor which would operate like this would be a property
object). In this case, the property's getter would be returning a string. As a formal 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)
I suppose that this class makes this conversion by means of defining __setattr__
method. You can read http://docs.python.org/2.7/reference/datamodel.html#customizing-attribute-access for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With