Author ncoghlan
Recipients ncoghlan, python-dev, rhettinger, serhiy.storchaka
Date 2015-01-04.09:20:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1420363225.41.0.277654433241.issue23132@psf.upfronthosting.co.za>
In-reply-to
Content
While I like the readability of Raymond's version, I think the main pay-off we're getting from the template based version is that each decorator invocation is creating *new* function objects.

That creation of new function objects is what allows Serhiy's patch to set __module__ and __qualname__ for each method implementation based on the class being defined.

The two approaches could be combined by moving the explicit definitions into factory functions that always created new function objects and set their introspection attributes appropriately. For example (untested code):

    def _fix_introspection(module, cls_qualname):
        def update_metadata(f):
            f.__qualname__ = "%s.%s" % (cls_qualname, f.__name__)
            f.__module__ = module
            return f
        return update_metadata

    def _derive_from_lt(module, cls_qualname):
        _NotImplemented = NotImplemented

        @_fix_introspection(module, cls_qualname)
        def __gt__(self, other):
            op_result = self.__lt__(other)
            if op_result is _NotImplemented:
                return _NotImplemented
            return not op_result and self != other

        @_fix_introspection(module, cls_qualname)
        def __le__(self, other):
            op_result = self.__lt__(other)
            return op_result or self == other

        @_fix_introspection(module, cls_qualname)
        def __ge__(self, other):
            op_result = self.__lt__(other)
            if op_result is _NotImplemented:
                return _NotImplemented
            return not op_result

        return __lt__, __gt__, __ge__
History
Date User Action Args
2015-01-04 09:20:25ncoghlansetrecipients: + ncoghlan, rhettinger, python-dev, serhiy.storchaka
2015-01-04 09:20:25ncoghlansetmessageid: <1420363225.41.0.277654433241.issue23132@psf.upfronthosting.co.za>
2015-01-04 09:20:25ncoghlanlinkissue23132 messages
2015-01-04 09:20:25ncoghlancreate