On Tue, Jun 16, 2020 at 2:00 AM Thomas Viehmann <t...@beamnet.de> wrote:

> Hello,
>
> thank you for your feedback!
> > I could think of a trick that inspect.getsource() might use if the class
> > contains at least one method: it could look at a method and try its
> > `__code__.co_filename` attribute (maybe only if the `__file__` attribute
> > for the module found via the class's `__module__` doesn't exist -- I'm
> sure
> > Jupyter can arrange for that to be the case). But I see how that would
> be a
> > problem (I can think of plenty of reasons why a class might not have any
> > methods).
>
> That is a great idea, in addition getsource would have to filter out
> inherited methods (which should be doable, but indeed exclude classes).
>

It would just have to iterate over the class `__dict__`, which doesn't
contain inherited objects anyways.


> Would you prefer such a patch to inspect.getsource over the adding
> __filename__?
>

It would certainly be much easier to get through the review process. Adding
a `__filename__` (why not `__file__`?) attribute to classes is a major
surgery, presumably requiring a PEP, and debating the pros and cons and
performance implications and fixing a bunch of tests that don't expect this
attribute, and so on. Adding an imperfect solution to inspect.getsource()
would only require the cooperation of whoever maintains the inspect module.


> > I do think that your proposal is reasonable, although I wonder what the
> > Jupyter developers think of it. (How closely are you connected to that
> > project?)
>
> I am not affiliated with Jupyter at all and I imagine that I'd be prone
> to asking "would it be nice if inspect.getsource worked better?", which
> likely doesn't yield the most interesting answers.
>

I had trouble parsing this sentence; I believe you mean to say that the
Jupyter maintainers would just tell you this should be fixed in
inspect.getsource()?


> We can chase the related reports:
>
> https://github.com/jupyter/notebook/issues/3802
>

This was closed because Jupyter is not to blame here at all, they declared
it an IPython issue.


> https://github.com/ipython/ipython/issues/11249
>

And here there also doesn't seem to be much interest, given that it's been
open and unanswered since 2018.

The topic does seem to pop up now and then:
>
>
> https://stackoverflow.com/questions/51566497/getting-the-source-of-an-object-defined-in-a-jupyter-notebook
>
> https://stackoverflow.com/questions/35854373/python-get-source-code-of-class-using-inspect
>

Very few stars. This suggests not many people care about this problem, and
that in turn might explain the lukewarm response you find everywhere.

Lastly, I have to ask: Why is this so important to you? What does this
prevent you from doing? You have illustrated the problem with toy examples
-- but what is the real-world problem you're encountering (apparently
regularly) that causes you to keep pushing on this? This needs to be
explored especially since so few other people appear to need this to work.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/Y6BZXEVK6QDSWC4ILKR2ZFCCOKZ3GXTM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to