[Python-ideas] Function-like Modules (better control/introspection)

Nick Coghlan ncoghlan at gmail.com
Mon Mar 17 18:38:47 CET 2014


On 18 Mar 2014 03:16, "Antoine Pitrou" <solipsis at pitrou.net> wrote:
>
> On Tue, 18 Mar 2014 02:36:31 +1000
> Nick Coghlan <ncoghlan at gmail.com> wrote:
> > On 18 Mar 2014 01:59, "C Anthony Risinger" <anthony at xtfx.me> wrote:
> > >
> > > ok, let me try this again, sans distractions:
> > >
> > > could we consider adding __code__ and __call__ to module objects?
> > __code__ would contain the code executed in the module __dict__, and
> > __call__ would reload the module.
> > >
> > > thoughts?
> >
> > The question to ask is: what would such a change make possible that
> > importlib doesn't already handle?
>
> I suppose module.__code__ would be a lot less wasteful than
> module.__loader__.get_code(module.__name__) (which seems to reload the
> bytecode from disk every time).

Yep, it's a space/speed trade-off - keeping this info around could waste a
lot of memory, and most code will never need it.

It's a similar reason to why we throw source code away after compiling it,
but then also route inspect.getsource() through the linecache module.

Cheers,
Nick.

>
> (while looking at this, I was surprised that dis.dis(some_module)
> 1) doesn't show a disassembly for the module toplevel 2) shows
> disassembly for objects which some_module imports from other modules)

I don't think we have any tests to see how dis.dis behaves with module
objects - it's likely just hitting the path that is designed to disassemble
all methods on a class. (We throw away the bytecode after execution for
classes as well)

Cheers,
Nick.

>
> Regards
>
> Antoine.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140318/18cfdc44/attachment.html>


More information about the Python-ideas mailing list