File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments