summaryrefslogtreecommitdiffstats
path: root/Lib/newdir.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-11-12 15:38:08 (GMT)
committerGuido van Rossum <guido@python.org>1991-11-12 15:38:08 (GMT)
commiteee9498b71a10381e5017201ed9d2fcb547d4305 (patch)
tree980a3f0e67dc1eac77d09edf8b4086904dfaa80d /Lib/newdir.py
parent5478cc68f8699dab10a25b976671ae52a0d8f6a8 (diff)
downloadcpython-eee9498b71a10381e5017201ed9d2fcb547d4305.zip
cpython-eee9498b71a10381e5017201ed9d2fcb547d4305.tar.gz
cpython-eee9498b71a10381e5017201ed9d2fcb547d4305.tar.bz2
Initial revision
Diffstat (limited to 'Lib/newdir.py')
-rw-r--r--Lib/newdir.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/Lib/newdir.py b/Lib/newdir.py
new file mode 100644
index 0000000..7164b98
--- /dev/null
+++ b/Lib/newdir.py
@@ -0,0 +1,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 (NameError, TypeError):
+ dictkeys = []
+ #
+ try:
+ methods = x.__methods__
+ except (NameError, TypeError):
+ methods = []
+ #
+ try:
+ members = x.__members__
+ except (NameError, TypeError):
+ members = []
+ #
+ try:
+ the_class = x.__class__
+ except (NameError, TypeError):
+ the_class = None
+ #
+ try:
+ bases = x.__bases__
+ except (NameError, 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