Commit 8b4c1753 authored by Guido van Rossum's avatar Guido van Rossum

getattr() is now built-in; no longer need a class to simulate varargs.

parent 3c95c360
# 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 # This should be the new dir(), except that it should still list
# the current local name space by default # the current local name space by default
#
def listattrs(x): def listattrs(x):
try: try:
dictkeys = x.__dict__.keys() dictkeys = x.__dict__.keys()
...@@ -59,21 +55,21 @@ def listattrs(x): ...@@ -59,21 +55,21 @@ def listattrs(x):
i = i+1 i = i+1
return total return total
# Helper to recognize functions # Helper to recognize functions
#
def is_function(x): def is_function(x):
return type(x) == type(is_function) 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. # 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. def dir(*args):
# if len(args) > 0:
class _dirclass: if len(args) == 1:
def dir(args): args = args[0]
if type(args) == type(()): return listattrs(args)
return listattrs(args[1])
else: else:
import __main__ import __main__
return listattrs(__main__) return listattrs(__main__)
dir = _dirclass().dir
# 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 # This should be the new dir(), except that it should still list
# the current local name space by default # the current local name space by default
#
def listattrs(x): def listattrs(x):
try: try:
dictkeys = x.__dict__.keys() dictkeys = x.__dict__.keys()
...@@ -59,21 +55,21 @@ def listattrs(x): ...@@ -59,21 +55,21 @@ def listattrs(x):
i = i+1 i = i+1
return total return total
# Helper to recognize functions # Helper to recognize functions
#
def is_function(x): def is_function(x):
return type(x) == type(is_function) 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. # 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. def dir(*args):
# if len(args) > 0:
class _dirclass: if len(args) == 1:
def dir(args): args = args[0]
if type(args) == type(()): return listattrs(args)
return listattrs(args[1])
else: else:
import __main__ import __main__
return listattrs(__main__) return listattrs(__main__)
dir = _dirclass().dir
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment