Django REST || Python? Class attributes as a key-value pair array
Question: I'm currently doing the [Django REST Framework tutorial](http://www.django-
rest-framework.org/tutorial/1-serialization/) and I came across something when
testing that confused me.

    
    
    >>> snippet =Snippet(code='foo = "bar"\n')
    >>> snippet.save()
    >>> snippet = Snippet(code='print "hello, world"\n')
    >>> snippet.save()
    >>> serializer = SnippetSerializer(snippet)
    >>> serializer.data
    >>> {'style': 'friendly', 'code': u'print "hello, world"\n', 'language': 'python', 'title': u'', 'linenos': False, 'pk': 2}
    

Specifically that last line. I don't define a `data` class or instance
attribute. The `SnippetSerializer` class is defined as the following:

    
    
    from django.forms import widgets
    from rest_framework import serializers
    from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
    
    
    
    class SnippetSerializer(serializers.Serializer):
        pk = serializers.IntegerField(read_only=True)
        title = serializers.CharField(required=False, allow_blank=True, max_length=100)
        code = serializers.CharField(style={'base_template': 'textarea.html'})
        linenos = serializers.BooleanField(required=False)
        language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
        style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')
    
        def create(self, validated_data):
            """
            Create and return a new 'Snippet' instance, given the validated data.
            """
            return Snippet.objects.create(**validated_data)
    
        def update(self, instance, validated_data):
            """
            Update and return an existing 'Snippet' instance, given the validated data.
            """
            instance.title = validated_data.get('title', instance.title)
            instance.code = validated_data.get('code', instance.code)
            instance.linenos = validated_data.get('linenos', instance.linenos)
            instance.language = validated_data.get('language', instance.language)
            instance.style = validated_data.get('style', instance.style)
            instance.save()
            return instance
    

I see that the `SnippetSerializer` class is relatives with
`serializers.Serializer` (right?), but it doesn't seem there is a `data`
attribute there either. It obviously works and allows me to access all of
`SnippetSerializers` attributes as a key-value pair array. I just couldn't
find documentation of this anywhere. Is `data` a reserved word in python? Is
this something unique to Django REST? Or is there some inheritance that I'm
not seeing?

Thank you in advance!


Answer: `data` is a property on the BaseSerializer class, from which Serializer
inherits. See [the code](https://github.com/tomchristie/django-rest-
framework/blob/master/rest_framework/serializers.py#L200).

