blob: 4994bf9100f032b04c34890bcbb5db259ba22bf6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# New dir() function and other attribute-related goodies
# This should become a built-in function
#
def getattr(x, name):
return eval('x.'+name)
# This should be the new dir(), except that it should still list
# the current local name space by default
#
def listattrs(x):
try:
dictkeys = x.__dict__.keys()
except (AttributeError, TypeError):
dictkeys = []
#
try:
methods = x.__methods__
except (AttributeError, TypeError):
methods = []
#
try:
members = x.__members__
except (AttributeError, TypeError):
members = []
#
try:
the_class = x.__class__
except (AttributeError, TypeError):
the_class = None
#
try:
bases = x.__bases__
except (AttributeError, TypeError):
bases = ()
#
total = dictkeys + methods + members
if the_class:
# It's a class instace; add the class's attributes
# that are functions (methods)...
class_attrs = listattrs(the_class)
class_methods = []
for name in class_attrs:
if is_function(getattr(the_class, name)):
class_methods.append(name)
total = total + class_methods
elif bases:
# It's a derived class; add the base class attributes
for base in bases:
base_attrs = listattrs(base)
total = total + base_attrs
total.sort()
return total
i = 0
while i+1 < len(total):
if total[i] == total[i+1]:
del total[i+1]
else:
i = i+1
return total
# Helper to recognize functions
#
def is_function(x):
return type(x) == type(is_function)
# Approximation of builtin dir(); this lists the user's
# variables by default, not the current local name space.
# Use a class method to make a function that can be called
# with or without arguments.
#
class _dirclass:
def dir(args):
if type(args) == type(()):
return listattrs(args[1])
else:
import __main__
return listattrs(__main__)
dir = _dirclass().dir
|