from-import on non-module objects?
clarence at netlojix.com
Sun Feb 4 16:33:42 EST 2001
On Sun, 04 Feb 2001, Tim Peters wrote:
>if an object supports getattr,
>it supports the only thing the "import" part of from-import really needs.
>Really bad idea (running under 2.1a2):
>>>> somelist = [1, 2, 3, 2, 1]
>>>> from somelist import count # blows up
>Traceback (most recent call last):
> File "<pyshell#2>", line 1, in ?
> from somelist import count # blows up
>ImportError: No module named somelist
>>>> import sys
>>>> sys.modules["somelist"] = somelist
>>>> from somelist import count # works, for some meaning of "works"
>>>> count(1), count(2), count(3), count(4)
>(2, 2, 1, 0)
Boy, was this confusing to someone who'd never heard of the count
method of lists! But firing up the friendly interactive Python and doing
a getattr(, 'count') showed it to me. Then I had to print its docstring to
figure out how that result was (2,2,1,0)!
Speaking of the friendly interactive Python, I saw recently that someone
finally made an interactive shell for Perl. I always wondered why they
hadn't done it sooner. [I'll bet it's not as good, though :)]
More information about the Python-list