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@231 | 22 | from native import _isinstance, _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 help(*args): |
paul@231 | 34 | |
paul@231 | 35 | # NOTE: This should show any docstring, but it is currently unsupported. |
paul@6 | 36 | |
paul@231 | 37 | pass |
paul@231 | 38 | |
paul@231 | 39 | def id(obj): |
paul@231 | 40 | |
paul@231 | 41 | # NOTE: This should show an object's address, but it is currently |
paul@231 | 42 | # NOTE: unsupported. |
paul@231 | 43 | |
paul@231 | 44 | pass |
paul@231 | 45 | |
paul@231 | 46 | def isclass(obj): |
paul@231 | 47 | |
paul@231 | 48 | "Return whether 'obj' is a class." |
paul@231 | 49 | |
paul@231 | 50 | return obj.__class__ is type |
paul@6 | 51 | |
paul@6 | 52 | def isinstance(obj, cls_or_tuple): |
paul@6 | 53 | |
paul@6 | 54 | """ |
paul@6 | 55 | Return whether 'obj' is an instance of 'cls_or_tuple', where the latter is |
paul@231 | 56 | either a class or a sequence of classes. |
paul@6 | 57 | """ |
paul@6 | 58 | |
paul@6 | 59 | if _isinstance(cls_or_tuple, tuple): |
paul@6 | 60 | for cls in cls_or_tuple: |
paul@231 | 61 | if obj.__class__ is cls or isclass(cls) and _isinstance(obj, cls): |
paul@6 | 62 | return True |
paul@6 | 63 | return False |
paul@6 | 64 | else: |
paul@231 | 65 | return obj.__class__ is cls_or_tuple or isclass(cls_or_tuple) and _isinstance(obj, cls_or_tuple) |
paul@231 | 66 | |
paul@231 | 67 | def issubclass(obj, cls_or_tuple): |
paul@231 | 68 | |
paul@231 | 69 | """ |
paul@231 | 70 | Return whether 'obj' is a class that is a subclass of 'cls_or_tuple', where |
paul@231 | 71 | the latter is either a class or a sequence of classes. If 'obj' is the same |
paul@231 | 72 | as the given class or classes, True is also returned. |
paul@231 | 73 | """ |
paul@6 | 74 | |
paul@231 | 75 | if not isclass(obj): |
paul@231 | 76 | return False |
paul@231 | 77 | elif _isinstance(cls_or_tuple, tuple): |
paul@231 | 78 | for cls in cls_or_tuple: |
paul@231 | 79 | if obj is cls or isclass(cls) and _issubclass(obj, cls): |
paul@231 | 80 | return True |
paul@231 | 81 | return False |
paul@231 | 82 | else: |
paul@231 | 83 | return obj is cls_or_tuple or isclass(cls_or_tuple) and _issubclass(obj, cls_or_tuple) |
paul@6 | 84 | |
paul@222 | 85 | def repr(obj): |
paul@222 | 86 | |
paul@222 | 87 | "Return a program representation for 'obj'." |
paul@222 | 88 | |
paul@248 | 89 | # Classes do not provide __repr__ directly. |
paul@248 | 90 | |
paul@248 | 91 | if isclass(obj): |
paul@268 | 92 | return obj.__name__ |
paul@248 | 93 | |
paul@248 | 94 | # Class attributes of instances provide __repr__. |
paul@248 | 95 | |
paul@248 | 96 | else: |
paul@248 | 97 | return obj.__repr__() |
paul@6 | 98 | |
paul@6 | 99 | # vim: tabstop=4 expandtab shiftwidth=4 |