n | class SizeDescriptor: | n | class Dsc: |
| def __get__(self, instance, owner): | | |
| | | def __get__(self, obj, cls): |
| | | #print(f"Get from {cls}:{obj}") |
| try: | | try: |
n | return len(instance) | n | return len(obj) |
| except BaseException: | | except BaseException: |
| try: | | try: |
n | return abs(instance) | n | return abs(obj) |
| except BaseException: | | except BaseException: |
| return 0 | | return 0 |
| | | |
| | | |
| def sizer(cls): | | def sizer(cls): |
n | cls.size = SizeDescriptor() | n | cls.size = Dsc() |
| return cls | | return cls |
| | | |
n | | n | # @sizer |
| | | # class S(str): |
| | | # pass |
| | | |
n | # if __name__ == "__main__": | n | |
| # @sizer | | # @sizer |
| # class S(str): | | |
| # pass | | |
| # | | |
| # | | |
| # @sizer | | |
| # class N(complex): | | # class N(complex): |
| # pass | | # pass |
| # | | |
| # | | |
| # @sizer | | |
| # class E(Exception): | | |
| # pass | | |
| # | | |
| # | | |
| # for obj in S("QWER"), N(3 + 4j), E("Exceptions know no lengths!"): | | |
| # print(obj, obj.size) | | |
| | | |
t | | t | # @sizer |
| | | # class E(Exception): |
| | | # pass |
| | | |
| | | # for obj in S("QWER"), N(3+4j), E("Exceptions know no lengths!"): |
| | | # print(obj, obj.size) |
| | | |