from-import on non-module objects?

Moshe Zadka moshez at zadka.site.co.il
Sun Feb 4 06:19:34 EST 2001


[Hamish Lawson]
> Have there been thoughts on having from-import used on non-module
> objects in order to introduce attributes from a given object into the
> current scope?

[Tim Roberts]
> Unless I misunderstood the explanation, I believe this exact thing
> is one of the features being included in Python 2.1.

[Tim Peters]
> The description of this change is the NEWS file for 2.1a1 and 2.1a2 was
> incomplete; it will be clearer in the next release.
<snip>
> Python has a long tradition of loosening type restrictions over time, and
> this was a natural part of that tradition:  if an object supports getattr,
> it supports the only thing the "import" part of from-import really needs.
> The "from" part still needs "a module name", though, but the operational
> definition of "a module name" is "a name that appears as a key in
> sys.modules".  So meet those two requirements, and Python couldn't care less
> whether it's "a real module" anymore.

Tim wrote a great explanation, and cute example, but he forgot to mention
one small thing: for
   from M import *
M, in addition to supporting getattr, must also either have an __all__
attribute which lists the names to be imported, or an __dict__ which
should be a dictionary whose keys are the names which will be important.

just-nitpicking-ly y'rs, Z.

-- 
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!
Fingerprint: 4BD1 7705 EEC0 260A 7F21  4817 C7FC A636 46D0 1BD6




More information about the Python-list mailing list