_sitebuiltins?

Steve D'Aprano steve+python at pearwood.info
Mon Sep 25 10:35:07 EDT 2017


On Mon, 25 Sep 2017 11:39 am, Stefan Ram wrote:


>   The problem with »__module__« is that it is not always
>   a permissible prefix for a use of the name. For example,

That's not what __module__ is intended for.

Just because an object obj was created inside module M doesn't mean it is
accessible as M.obj. __module__ is not a promise that "obj" is a name in that
namespace.

__module__ is a dunder attribute. That means, unless otherwise documented, it
exists for the use of the Python interpreter, not client code.

[...]
> |>>> builtins.help
> |Type help() for interactive help, or help(object) for help about object.
> 
>   So, I am looking for something that gives me »builtins«
>   in the case of »help«.

You won't find one, because `help` is injected into the builtins module at
runtime, but the site.py module. If you read the source of site.py, you will
find the code that does the injection, but there's no attribute of `help` that
will tell you this.

Consider:

py> import statistics, builtins  # Python 2 use __builtin__
py> builtins.average = statistics.mean
py> assert average is statistics.mean

Given that average, in the builtins module, is the same function object as mean,
in the statistics module, what should average.__module__ say?

py> average.__module__
'statistics'



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list