New submission from Aaron Christianson <ninjaa...@gmail.com>: I'm always writting these wrapper classes where I want to selectively want to expose the interface of some of the methods of certain attributes to co the containing object. This can mean I spend a lot of time implementing wrapper methods. That's no good. I wrote a class decorator to make this easy, and I realized it's a perfect complement to the new dataclasses module, though it can also be used with normal classes. I figured I'd check if you're interested in that. The interface looks like this:
>>> from dataclasses import dataclass, acts_like >>> @acts_like('weight', ['__add__']) ... @acts_like('still_fresh', ['__bool__']) ... @dataclass ... class Spam: ... weight: int ... still_fresh: bool >>> s = Spam(42, False) >>> s + 3 45 >>> if not s: ... print('the spam is bad') the spam is bad It's a handy way to build objects with composition, but still get some of the benefits of inheritance in a *selective* and *explicite* way. Here's the code: https://github.com/ninjaaron/cpython/blob/acts_like/Lib/dataclasses.py#L978 May require some addtional twiddling to make it work with frozen dataclasses, but I don't think it should be a problem. ---------- components: Library (Lib) messages: 313096 nosy: eric.smith, ninjaaron priority: normal severity: normal status: open title: added acts_like decorator to dataclasses module type: enhancement versions: Python 3.7 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue32977> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com