Mercurial > cpython-lru_cache-weakref
annotate Lib/newdir.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 1940892d2fe3 |
| children |
| rev | line source |
|---|---|
|
783
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
1 # New dir() function |
| 430 | 2 |
| 3 | |
| 4 # This should be the new dir(), except that it should still list | |
| 5 # the current local name space by default | |
|
783
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
6 |
| 430 | 7 def listattrs(x): |
| 8 try: | |
| 9 dictkeys = x.__dict__.keys() | |
| 525 | 10 except (AttributeError, TypeError): |
| 430 | 11 dictkeys = [] |
| 12 # | |
| 13 try: | |
| 14 methods = x.__methods__ | |
| 525 | 15 except (AttributeError, TypeError): |
| 430 | 16 methods = [] |
| 17 # | |
| 18 try: | |
| 19 members = x.__members__ | |
| 525 | 20 except (AttributeError, TypeError): |
| 430 | 21 members = [] |
| 22 # | |
| 23 try: | |
| 24 the_class = x.__class__ | |
| 525 | 25 except (AttributeError, TypeError): |
| 430 | 26 the_class = None |
| 27 # | |
| 28 try: | |
| 29 bases = x.__bases__ | |
| 525 | 30 except (AttributeError, TypeError): |
| 430 | 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): | |
| 559 | 52 if total[i] == total[i+1]: |
| 430 | 53 del total[i+1] |
| 54 else: | |
| 55 i = i+1 | |
| 56 return total | |
| 57 | |
|
783
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
58 |
| 430 | 59 # Helper to recognize functions |
|
783
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
60 |
| 430 | 61 def is_function(x): |
| 559 | 62 return type(x) == type(is_function) |
| 430 | 63 |
|
783
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
64 |
|
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
65 # Approximation of builtin dir(); but note that this lists the user's |
| 430 | 66 # variables by default, not the current local name space. |
|
783
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
67 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
783
diff
changeset
|
68 def dir(x = None): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
783
diff
changeset
|
69 if x is not None: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
783
diff
changeset
|
70 return listattrs(x) |
|
783
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
71 else: |
|
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
72 import __main__ |
|
3e1fbe84cf9f
getattr() is now built-in; no longer need a class to simulate varargs.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
73 return listattrs(__main__) |
