-
-
Save nikitakit/642cb96febdf2f812d0b to your computer and use it in GitHub Desktop.
Inspecting interactively created classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect, sys | |
def new_getfile(object, _old_getfile=inspect.getfile): | |
if not inspect.isclass(object): | |
return _old_getfile(object) | |
# Lookup by parent module (as in current inspect) | |
if hasattr(object, '__module__'): | |
object_ = sys.modules.get(object.__module__) | |
if hasattr(object_, '__file__'): | |
return object_.__file__ | |
# If parent module is __main__, lookup by methods (NEW) | |
for name, member in inspect.getmembers(object): | |
if inspect.isfunction(member) and object.__qualname__ + '.' + member.__name__ == member.__qualname__: | |
return inspect.getfile(member) | |
else: | |
raise TypeError('Source for {!r} not found'.format(object)) | |
inspect.getfile = new_getfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In standard Python (3.5.0) shell the result of a quick test is "". I assume the shell didn't
linecache
that.