|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# File listinstance.py (2.x + 3.x) |
| 3 | + |
| 4 | +from __future__ import print_function |
| 5 | + |
| 6 | + |
| 7 | +class ListInstance: |
| 8 | + """ |
| 9 | + Mix-in class that provides a formatted print() or str() of instances via |
| 10 | + inheritance of __str__ coded here; displays instance attrs only; self is |
| 11 | + instance of lowest class; __X names avoid clashing with client's attrs |
| 12 | + """ |
| 13 | + |
| 14 | + def __attrnames(self): |
| 15 | + result = '' |
| 16 | + for attr in sorted(self.__dict__): |
| 17 | + result += '\t%s=%s\n' % (attr, self.__dict__[attr]) |
| 18 | + return result |
| 19 | + |
| 20 | + def __str__(self): |
| 21 | + return '<Instance of %s, address %s:\n%s>' % ( |
| 22 | + self.__class__.__name__, |
| 23 | + id(self), |
| 24 | + self.__attrnames()) |
| 25 | + |
| 26 | + |
| 27 | +def tester(listerclass, sept=False): |
| 28 | + |
| 29 | + class Super: |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + self.data1 = 'spam' |
| 33 | + |
| 34 | + def ham(self): |
| 35 | + pass |
| 36 | + |
| 37 | + class Sub(Super, listerclass): |
| 38 | + |
| 39 | + def __init__(self): |
| 40 | + Super.__init__(self) |
| 41 | + self.data2 = 'eggs' |
| 42 | + self.data3 = 42 |
| 43 | + |
| 44 | + def spam(self): |
| 45 | + pass |
| 46 | + |
| 47 | + instance = Sub() |
| 48 | + print(instance) |
| 49 | + if sept: |
| 50 | + print('-' * 80) |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + |
| 54 | + tester(ListInstance) |
0 commit comments