[Python-ideas] Adding an "export" decorator in (e.g.) functools
Chris Angelico
rosuav at gmail.com
Fri May 9 10:17:16 CEST 2014
On Fri, May 9, 2014 at 6:03 PM, Bill Winslow <bunslow at gmail.com> wrote:
>
> On Fri, May 9, 2014 at 2:38 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>
>> (By the way: You responded to two different posts in yours, and didn't
>> cite either of them. Please keep the original-poster line, such as
>> you'll see on the next non-blank line.)
>
> Sorry -- I think this is correct? I'm new to mailing lists :P
Yep, looks good! Thanks!
You'll find this sort of thing helpful even with non-list mail, too.
As soon as you get a long and complex discussion (particularly between
more than two people), it's useful to bottom-post, trim quotes,
maintain proper citations, etc, etc. Good habits to be in.
> That's what I had basically just got working, except with exec instead of
> just straight up modifying the globals... and if you're going to do that,
> you may as well directly assign globls['export'] = grabber :P (still
> better than an exec of course :D).
You could do it that way too, yes. That would shorten the usage a little.
> On the other hand, while testing my version of the above (with exec), I ran
> into another issue (or at least I perceive it as such): the import stuff
> only respects __all__ *if* we are importing * from the module. If instead we
> do "import module as m", the *entire* namespace of the module is made
> available in m, even those that start with an underscore.
>
> Can someone please explain the rationale behind that? I would consider this
> surprising. Why should "import module" give different results than "from
> module import *"? If the latter can be author-limited, why not the former as
> well? Even a pointer to relevant documentation would be helpful.
>
> (Basically, I had this idea because I thought that __all__ was way more
> important than it apparently is.)
Yep. When you "import module" (optionally "as m"), you can reference
whatever you want; __all__ is to help tame an "import * from". But
neither of them can truly hide anything; Python doesn't work that way.
You can always go digging around and finding the internals of
something. The nearest you can get to truly hiding something from the
namespace is to del it when you're done, which obviously means you
can't reference it yourself either. (Has its uses, though; for
instance, you might undefine export when you're done with it, in your
above examples.)
ChrisA
More information about the Python-ideas
mailing list