1.1 --- a/lib/__builtins__/identity.py Thu Nov 24 00:58:36 2016 +0100
1.2 +++ b/lib/__builtins__/identity.py Thu Nov 24 18:55:10 2016 +0100
1.3 @@ -19,31 +19,68 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from native import _isinstance
1.8 +from native import _isinstance, _issubclass
1.9 +
1.10 +def callable(obj):
1.11 +
1.12 + "Return whether 'obj' is callable."
1.13 +
1.14 + # NOTE: Classes and functions are callable, modules are not callable,
1.15 + # NOTE: only instances with __call__ methods should be callable.
1.16 +
1.17 + pass
1.18 +
1.19 +def help(*args):
1.20 +
1.21 + # NOTE: This should show any docstring, but it is currently unsupported.
1.22
1.23 -def callable(obj): pass
1.24 -def help(*args): pass
1.25 -def id(obj): pass
1.26 + pass
1.27 +
1.28 +def id(obj):
1.29 +
1.30 + # NOTE: This should show an object's address, but it is currently
1.31 + # NOTE: unsupported.
1.32 +
1.33 + pass
1.34 +
1.35 +def isclass(obj):
1.36 +
1.37 + "Return whether 'obj' is a class."
1.38 +
1.39 + return obj.__class__ is type
1.40
1.41 def isinstance(obj, cls_or_tuple):
1.42
1.43 """
1.44 Return whether 'obj' is an instance of 'cls_or_tuple', where the latter is
1.45 - either a class or a tuple of classes.
1.46 + either a class or a sequence of classes.
1.47 """
1.48
1.49 - # NOTE: CPython insists on tuples, but any sequence might be considered
1.50 - # NOTE: acceptable.
1.51 -
1.52 if _isinstance(cls_or_tuple, tuple):
1.53 for cls in cls_or_tuple:
1.54 - if obj.__class__ is cls or _isinstance(obj, cls):
1.55 + if obj.__class__ is cls or isclass(cls) and _isinstance(obj, cls):
1.56 return True
1.57 return False
1.58 else:
1.59 - return obj.__class__ is cls_or_tuple or _isinstance(obj, cls_or_tuple)
1.60 + return obj.__class__ is cls_or_tuple or isclass(cls_or_tuple) and _isinstance(obj, cls_or_tuple)
1.61 +
1.62 +def issubclass(obj, cls_or_tuple):
1.63 +
1.64 + """
1.65 + Return whether 'obj' is a class that is a subclass of 'cls_or_tuple', where
1.66 + the latter is either a class or a sequence of classes. If 'obj' is the same
1.67 + as the given class or classes, True is also returned.
1.68 + """
1.69
1.70 -def issubclass(obj, cls_or_tuple): pass
1.71 + if not isclass(obj):
1.72 + return False
1.73 + elif _isinstance(cls_or_tuple, tuple):
1.74 + for cls in cls_or_tuple:
1.75 + if obj is cls or isclass(cls) and _issubclass(obj, cls):
1.76 + return True
1.77 + return False
1.78 + else:
1.79 + return obj is cls_or_tuple or isclass(cls_or_tuple) and _issubclass(obj, cls_or_tuple)
1.80
1.81 def repr(obj):
1.82