Guido van Rossum | 7084ec8 | 1992-04-02 10:37:02 +0000 | [diff] [blame] | 1 | # New dir() function |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 3 | |
| 4 | # This should be the new dir(), except that it should still list |
| 5 | # the current local name space by default |
Guido van Rossum | 7084ec8 | 1992-04-02 10:37:02 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 7 | def listattrs(x): |
| 8 | try: |
| 9 | dictkeys = x.__dict__.keys() |
Guido van Rossum | 946749f | 1991-12-26 13:04:02 +0000 | [diff] [blame] | 10 | except (AttributeError, TypeError): |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 11 | dictkeys = [] |
| 12 | # |
| 13 | try: |
| 14 | methods = x.__methods__ |
Guido van Rossum | 946749f | 1991-12-26 13:04:02 +0000 | [diff] [blame] | 15 | except (AttributeError, TypeError): |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 16 | methods = [] |
| 17 | # |
| 18 | try: |
| 19 | members = x.__members__ |
Guido van Rossum | 946749f | 1991-12-26 13:04:02 +0000 | [diff] [blame] | 20 | except (AttributeError, TypeError): |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 21 | members = [] |
| 22 | # |
| 23 | try: |
| 24 | the_class = x.__class__ |
Guido van Rossum | 946749f | 1991-12-26 13:04:02 +0000 | [diff] [blame] | 25 | except (AttributeError, TypeError): |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 26 | the_class = None |
| 27 | # |
| 28 | try: |
| 29 | bases = x.__bases__ |
Guido van Rossum | 946749f | 1991-12-26 13:04:02 +0000 | [diff] [blame] | 30 | except (AttributeError, TypeError): |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 31 | bases = () |
| 32 | # |
| 33 | total = dictkeys + methods + members |
| 34 | if the_class: |
| 35 | # It's a class instace; add the class's attributes |
| 36 | # that are functions (methods)... |
| 37 | class_attrs = listattrs(the_class) |
| 38 | class_methods = [] |
| 39 | for name in class_attrs: |
| 40 | if is_function(getattr(the_class, name)): |
| 41 | class_methods.append(name) |
| 42 | total = total + class_methods |
| 43 | elif bases: |
| 44 | # It's a derived class; add the base class attributes |
| 45 | for base in bases: |
| 46 | base_attrs = listattrs(base) |
| 47 | total = total + base_attrs |
| 48 | total.sort() |
| 49 | return total |
| 50 | i = 0 |
| 51 | while i+1 < len(total): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 52 | if total[i] == total[i+1]: |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 53 | del total[i+1] |
| 54 | else: |
| 55 | i = i+1 |
| 56 | return total |
| 57 | |
Guido van Rossum | 7084ec8 | 1992-04-02 10:37:02 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 59 | # Helper to recognize functions |
Guido van Rossum | 7084ec8 | 1992-04-02 10:37:02 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 61 | def is_function(x): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 62 | return type(x) == type(is_function) |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 7084ec8 | 1992-04-02 10:37:02 +0000 | [diff] [blame] | 64 | |
| 65 | # Approximation of builtin dir(); but note that this lists the user's |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 66 | # variables by default, not the current local name space. |
Guido van Rossum | 7084ec8 | 1992-04-02 10:37:02 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | def dir(x = None): |
| 69 | if x is not None: |
| 70 | return listattrs(x) |
Guido van Rossum | 7084ec8 | 1992-04-02 10:37:02 +0000 | [diff] [blame] | 71 | else: |
| 72 | import __main__ |
| 73 | return listattrs(__main__) |