On Fri, Feb 10, 2017 at 9:20 AM, Nick Coghlan <[email protected]> wrote:
> What I would personally hope to see from the proposal is that given: > > class Spam: > pass > > def Spam.func(self): > return __class__ > > the effective runtime behaviour would be semantically identical to: > > class Spam: > def func(self): > return __class__ > Yes, this is exactly what I would hope/expect to see. One use case for this functionality is defining classes with an extensive method-based API with a sane dependency graph. For example, consider writing a class like numpy.ndarray <https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html> or pandas.DataFrame <http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html> with dozens of methods. You could argue that using so many methods is an anti-pattern, but nonetheless it's pretty common and hard to avoid in some cases (e.g., for making number-like classes that support arithmetic and comparisons). For obvious reasons, the functionality for these classes does not all live in a single module. But the modules that define helper functions for most methods also depend on the base class, so many of them need to get imported inside method definitions <https://github.com/pandas-dev/pandas/blob/v0.19.2/pandas/core/frame.py#L1227> to avoid circular imports. The result is pretty ugly, and files defining the class still get gigantic. An important note is that ideally, we would still have way of indicating that Spam.func should exists in on the Spam class itself, even if it doesn't define the implementation. I suppose an abstractmethod overwritten by the later definition might do the trick, e.g., class Spam(metaclass=ABCMeta): @abstractmethod def func(self): pass def Spam.func(self): return __class__ And finally, it's quite possible that there's a clean metaclass based solution for extending Spam in another file, I just don't know it yet.
_______________________________________________ Python-ideas mailing list [email protected] https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
