from fields import FieldBase

class Model(object):

    class __metaclass__(type):
        def __init__(cls, name, bases, attrs):
            fields = {}
            for name, value in attrs.items():
                if isinstance(value, FieldBase):
                    fields[name] = value
            setattr(cls, '_fields', fields)

    def __init__(self, data):
        """Create an instance of the model subclass. The constructor should
        be passed a dictionary of data to provide values for its fields.

        For each field defined on the class (and stored in the _fields
        property by the metaclass), a property is created on the instance
        which contains the converted value from the source data.
        """
        for name, field in self._fields.items():
            key = field.source or name
            field.populate(data.get(key))
            value = field.to_python()
            setattr(self, name, value)
        self._data = data

from fields import FieldBase

class Model(object):

    class __metaclass__(type):
        def __init__(cls, name, bases, attrs):
            cls._fields = {}
            for name, value in attrs.items():
                if isinstance(value, FieldBase):
                    cls._fields[name] = value

    def __init__(self, data):
        """Create an instance of the model subclass. The constructor should
        be passed a dictionary of data to provide values for its fields.

        For each field defined on the class (and stored in the _fields
        property by the metaclass), a property is created on the instance
        which contains the converted value from the source data.
        """
        for name, field in self._fields.items():
            key = field.source or name
            field.populate(data.get(key))
            value = field.to_python()
            setattr(self, name, value)
        self._data = data

Change to make metaclass clearer
Change to make metaclass clearer

  from fields import FieldBase
  
  class Model(object):
  
      class __metaclass__(type):
          def __init__(cls, name, bases, attrs):
-             fields = {}
+             cls._fields = {}
              for name, value in attrs.items():
                  if isinstance(value, FieldBase):
-                     fields[name] = value
+                     cls._fields[name] = value
-             setattr(cls, '_fields', fields)
  
      def __init__(self, data):
          """Create an instance of the model subclass. The constructor should
          be passed a dictionary of data to provide values for its fields.
  
          For each field defined on the class (and stored in the _fields
          property by the metaclass), a property is created on the instance
          which contains the converted value from the source data.
          """
          for name, field in self._fields.items():
              key = field.source or name
              field.populate(data.get(key))
              value = field.to_python()
              setattr(self, name, value)
          self._data = data
  
Change to make metaclass clearer
## Code Before:
from fields import FieldBase

class Model(object):

    class __metaclass__(type):
        def __init__(cls, name, bases, attrs):
            fields = {}
            for name, value in attrs.items():
                if isinstance(value, FieldBase):
                    fields[name] = value
            setattr(cls, '_fields', fields)

    def __init__(self, data):
        """Create an instance of the model subclass. The constructor should
        be passed a dictionary of data to provide values for its fields.

        For each field defined on the class (and stored in the _fields
        property by the metaclass), a property is created on the instance
        which contains the converted value from the source data.
        """
        for name, field in self._fields.items():
            key = field.source or name
            field.populate(data.get(key))
            value = field.to_python()
            setattr(self, name, value)
        self._data = data

## Instruction:
Change to make metaclass clearer
## Code After:
from fields import FieldBase

class Model(object):

    class __metaclass__(type):
        def __init__(cls, name, bases, attrs):
            cls._fields = {}
            for name, value in attrs.items():
                if isinstance(value, FieldBase):
                    cls._fields[name] = value

    def __init__(self, data):
        """Create an instance of the model subclass. The constructor should
        be passed a dictionary of data to provide values for its fields.

        For each field defined on the class (and stored in the _fields
        property by the metaclass), a property is created on the instance
        which contains the converted value from the source data.
        """
        for name, field in self._fields.items():
            key = field.source or name
            field.populate(data.get(key))
            value = field.to_python()
            setattr(self, name, value)
        self._data = data
