Lichen

Annotated lib/__builtins__/identity.py

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