File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- # New dir() function and other attribute-related goodies
1+ # New dir() function
22
3- # This should become a built-in function
4- #
5- def getattr (x , name ):
6- return eval ('x.' + name )
73
84# This should be the new dir(), except that it should still list
95# the current local name space by default
10- #
6+
117def listattrs (x ):
128 try :
139 dictkeys = x .__dict__ .keys ()
@@ -59,21 +55,21 @@ def listattrs(x):
5955 i = i + 1
6056 return total
6157
58+
6259# Helper to recognize functions
63- #
60+
6461def is_function (x ):
6562 return type (x ) == type (is_function )
6663
67- # Approximation of builtin dir(); this lists the user's
64+
65+ # Approximation of builtin dir(); but note that this lists the user's
6866# variables by default, not the current local name space.
69- # Use a class method to make a function that can be called
70- # with or without arguments.
71- #
72- class _dirclass :
73- def dir (args ):
74- if type (args ) == type (()):
75- return listattrs (args [1 ])
76- else :
77- import __main__
78- return listattrs (__main__ )
79- dir = _dirclass ().dir
67+
68+ def dir (* args ):
69+ if len (args ) > 0 :
70+ if len (args ) == 1 :
71+ args = args [0 ]
72+ return listattrs (args )
73+ else :
74+ import __main__
75+ return listattrs (__main__ )
Original file line number Diff line number Diff line change 1- # New dir() function and other attribute-related goodies
1+ # New dir() function
22
3- # This should become a built-in function
4- #
5- def getattr (x , name ):
6- return eval ('x.' + name )
73
84# This should be the new dir(), except that it should still list
95# the current local name space by default
10- #
6+
117def listattrs (x ):
128 try :
139 dictkeys = x .__dict__ .keys ()
@@ -59,21 +55,21 @@ def listattrs(x):
5955 i = i + 1
6056 return total
6157
58+
6259# Helper to recognize functions
63- #
60+
6461def is_function (x ):
6562 return type (x ) == type (is_function )
6663
67- # Approximation of builtin dir(); this lists the user's
64+
65+ # Approximation of builtin dir(); but note that this lists the user's
6866# variables by default, not the current local name space.
69- # Use a class method to make a function that can be called
70- # with or without arguments.
71- #
72- class _dirclass :
73- def dir (args ):
74- if type (args ) == type (()):
75- return listattrs (args [1 ])
76- else :
77- import __main__
78- return listattrs (__main__ )
79- dir = _dirclass ().dir
67+
68+ def dir (* args ):
69+ if len (args ) > 0 :
70+ if len (args ) == 1 :
71+ args = args [0 ]
72+ return listattrs (args )
73+ else :
74+ import __main__
75+ return listattrs (__main__ )
You can’t perform that action at this time.
0 commit comments