Skip to content

Commit 90d3f4f

Browse files
committed
Added classtools
1 parent c0e8112 commit 90d3f4f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

classtools.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# File classtools.py (new)
2+
"Assorted class utilities and tools"
3+
4+
class AttrDisplay:
5+
"""
6+
Provide an inheritable display overload method that shows
7+
instance with their class name and a name=value pair for
8+
each attribute stored on the instance itself (but not attrs
9+
inherited from its classes). Can be mixed into any class,
10+
and will work on any instance
11+
"""
12+
def __gatherAttrs(self):
13+
attrs = []
14+
for key in sorted(self.__dict__):
15+
attrs.append("%s=%s" % (key, getattr(self, key)))
16+
return attrs
17+
18+
def __repr__(self):
19+
return "[%s: %s]" % (self.__class__.__name__, self.__gatherAttrs())
20+
21+
if __name__ == "__main__":
22+
23+
class TopTest(AttrDisplay):
24+
count = 0
25+
26+
def __init__(self):
27+
self.attr1 = TopTest.count
28+
self.attr2 = TopTest.count+1
29+
TopTest.count += 2
30+
31+
class SubTest(TopTest):
32+
pass
33+
34+
X, Y = TopTest(), SubTest()
35+
print(X)
36+
print(Y)

0 commit comments

Comments
 (0)