Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7084ec8

Browse files
committed
getattr() is now built-in; no longer need a class to simulate varargs.
1 parent 60279da commit 7084ec8

2 files changed

Lines changed: 30 additions & 38 deletions

File tree

Lib/lib-old/newdir.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
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+
117
def 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+
6461
def 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__)

Lib/newdir.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
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+
117
def 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+
6461
def 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__)

0 commit comments

Comments
 (0)