Lichen

lib/__builtins__/identity.py

775:c8ba74a474eb
2017-03-25 Paul Boddie Fixed method name.
     1 #!/usr/bin/env python     2      3 """     4 Identity-related functions.     5      6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from native import isinstance as _isinstance, issubclass as _issubclass    23     24 def callable(obj):    25     26     "Return whether 'obj' is callable."    27     28     # NOTE: Classes and functions are callable, modules are not callable,    29     # NOTE: only instances with __call__ methods should be callable.    30     31     pass    32     33 def id(obj):    34     35     # NOTE: This should show an object's address, but it is currently    36     # NOTE: unsupported.    37     38     pass    39     40 def isclass(obj):    41     42     "Return whether 'obj' is a class."    43     44     return obj.__class__ is type    45     46 def isinstance(obj, cls_or_tuple):    47     48     """    49     Return whether 'obj' is an instance of 'cls_or_tuple', where the latter is    50     either a class or a sequence of classes.    51     """    52     53     if _isinstance(cls_or_tuple, tuple):    54         for cls in cls_or_tuple:    55             if obj.__class__ is cls or isclass(cls) and _isinstance(obj, cls):    56                 return True    57         return False    58     else:    59         return obj.__class__ is cls_or_tuple or isclass(cls_or_tuple) and _isinstance(obj, cls_or_tuple)    60     61 def issubclass(obj, cls_or_tuple):    62     63     """    64     Return whether 'obj' is a class that is a subclass of 'cls_or_tuple', where    65     the latter is either a class or a sequence of classes. If 'obj' is the same    66     as the given class or classes, True is also returned.    67     """    68     69     if not isclass(obj):    70         return False    71     elif _isinstance(cls_or_tuple, tuple):    72         for cls in cls_or_tuple:    73             if obj is cls or isclass(cls) and _issubclass(obj, cls):    74                 return True    75         return False    76     else:    77         return obj is cls_or_tuple or isclass(cls_or_tuple) and _issubclass(obj, cls_or_tuple)    78     79 def repr(obj):    80     81     "Return a program representation for 'obj'."    82     83     # Class attributes of instances provide __repr__.    84     85     return obj.__repr__()    86     87 # vim: tabstop=4 expandtab shiftwidth=4