import six
import types


def _get_definition_module(obj):
    """
    Get the name of the module that an object is defined in, or ``None`` if
    unknown.

    For classes and functions, this returns the ``__module__`` attribute.

    For object instances, this returns ``None``, ignoring the ``__module__``
    attribute.  The reason is that the ``__module__`` attribute on an instance
    just gives the module that the class was defined in, which is not
    necessarily the module where the instance was constructed.

    :rtype:
      ``str``
    """
    if isinstance(obj, (type, six.class_types, types.FunctionType,
                        types.MethodType)):
        return getattr(obj, "__module__", None)
    else:
        return None