bpo-33826: add __filename__ to Python-defined classes for introspection#13894
bpo-33826: add __filename__ to Python-defined classes for introspection#13894t-vi wants to merge 1 commit intopython:masterfrom
Conversation
This adds an attribute __filename__ to classes. It enables introspection in IPython using inspect.getsource().
|
I'm sorry, I don't read consensus in the ticket that this is the solution. You may have to make a quick detour through python-dev or Discourse (discuss.python.org) to see what other core devs think. |
|
I'll do that next year. Thanks! |
|
So a year passed, I sent the following to python-dev, but I'm not sure whether it is filtered (it doesn't show up in the archives). I would love to hear your opinion on the following aspect of inspect that I believe might be worth improving: Consider the following program saved in a file (say import inspect
def hello():
print("Hello World")
print(inspect.getsource(hello))
class Hello:
def __init__(self):
print("Hello World")
print(inspect.getsource(Hello))Running Now, some of us use an Jupyter (with the capabilities provided by IPython) notebooks, which are a great tool and awesome match with Python. These notebooks can be large and complex enough to want to use introspection on methods defined in itself (also, I'm prototyping things I might want to use as a library in Notebooks a lot, and I think I'm not alone). IPython enhances the interactive console to enable introspection (by providing "files" for the cells). def hello():
print("Hello World")
print(inspect.getsource(hello))However, it does not work for classes: class Hello:
def __init__(self):
print("Hello World")
print(inspect.getsource(Hello))will run into an error in a Jupyter notebook, more precisely The reason why the latter does not work is because inspect cannot find a source file. The technical background is that for a function I once made this PR in github and sent a bug and patch earlier but got, let's say, reactions that were not entirely encouraging. I still think that it is a useful feature and I don't think that there are readily available solutions and after another year has passed, I humbly submit this for your considerations. |
|
Oh, your message showed up. It's here: https://mail.python.org/archives/list/python-dev@python.org/message/AC5B5VORS6FF6KJ3EG5DIV3ZLAQB6DIY/ |
This adds an attribute
__filename__to classes. It enables introspection in IPython/Jupyter using inspect.getsource().https://bugs.python.org/issue33826