Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61f98a9

Browse files
Same as listinherited.py, But more formatted
1 parent 99097ed commit 61f98a9

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

‎listinherited2.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
# File listinherited.py (2.x + 3.x)
3+
4+
5+
class ListInherited:
6+
"""
7+
Use dir() to collection both instance attr and names inherited from
8+
its classes; Python 3.x shows more name than 2.x because of the
9+
implied object superclass in the new-style class model; getattr()
10+
fetches inherited names not in self.__dict__; user __str__, not
11+
__repr__, or else the loops when printing bound method!
12+
"""
13+
14+
def __attrname(self, indent=' '*4):
15+
result = 'Unders%s\n%s%%s\nOthers%s\n' % ('-'*77, indent, '-'*77)
16+
unders = []
17+
for attr in dir(self): # instance dir
18+
if attr[:2] == '__' and attr[-2:] == '__':
19+
unders.append(attr)
20+
else:
21+
display = str(getattr(self, attr))[:82-(len(indent) + len(attr))]
22+
result += '%s%s=%s\n' % (indent, attr, display)
23+
return result % ', '.join(unders)
24+
25+
def __str__(self):
26+
return '<Instance of %s, address %s:\n%s>' % (
27+
self.__class__.__name__,
28+
id(self), # My address
29+
self.__attrname()) # name=value list
30+
31+
32+
def tester(listerclass, sept=False):
33+
34+
class Super:
35+
36+
def __init__(self):
37+
self.data1 = 'spam'
38+
39+
def ham(self):
40+
pass
41+
42+
class Sub(Super, listerclass):
43+
44+
def __init__(self):
45+
Super.__init__(self)
46+
self.data2 = 'eggs'
47+
self.data3 = 42
48+
49+
def spam(self):
50+
pass
51+
52+
instance = Sub()
53+
print(instance)
54+
if sept:
55+
print('-' * 80)
56+
57+
58+
if __name__ == '__main__':
59+
60+
tester(ListInherited)
61+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /