diff options
author | Guido van Rossum <guido@python.org> | 1992-04-02 10:37:02 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1992-04-02 10:37:02 (GMT) |
commit | 7084ec8167f1e8dfa2104c30f52007b15b89e6dc (patch) | |
tree | 3f7bef54e9becf9bf39683da965cbbb9c9169cf8 | |
parent | 60279da7f5e6ca52b27ac91068e000b85ff3a671 (diff) | |
download | cpython-7084ec8167f1e8dfa2104c30f52007b15b89e6dc.zip cpython-7084ec8167f1e8dfa2104c30f52007b15b89e6dc.tar.gz cpython-7084ec8167f1e8dfa2104c30f52007b15b89e6dc.tar.bz2 |
getattr() is now built-in; no longer need a class to simulate varargs.
-rw-r--r-- | Lib/lib-old/newdir.py | 34 | ||||
-rw-r--r-- | Lib/newdir.py | 34 |
2 files changed, 30 insertions, 38 deletions
diff --git a/Lib/lib-old/newdir.py b/Lib/lib-old/newdir.py index 4994bf9..26a7df0 100644 --- a/Lib/lib-old/newdir.py +++ b/Lib/lib-old/newdir.py @@ -1,13 +1,9 @@ -# New dir() function and other attribute-related goodies +# New dir() function -# 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() @@ -59,21 +55,21 @@ def listattrs(x): 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 + +# Approximation of builtin dir(); but note that 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 + +def dir(*args): + if len(args) > 0: + if len(args) == 1: + args = args[0] + return listattrs(args) + else: + import __main__ + return listattrs(__main__) diff --git a/Lib/newdir.py b/Lib/newdir.py index 4994bf9..26a7df0 100644 --- a/Lib/newdir.py +++ b/Lib/newdir.py @@ -1,13 +1,9 @@ -# New dir() function and other attribute-related goodies +# New dir() function -# 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() @@ -59,21 +55,21 @@ def listattrs(x): 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 + +# Approximation of builtin dir(); but note that 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 + +def dir(*args): + if len(args) > 0: + if len(args) == 1: + args = args[0] + return listattrs(args) + else: + import __main__ + return listattrs(__main__) |