[Python-Dev] __import__ problems

Christian Heimes lists at cheimes.de
Fri Nov 28 17:30:49 CET 2008


Steven D'Aprano wrote:
> However, having disagreed with your arguments, I will say this: I think 
> a "tail" argument to __import__ would be more elegant:
> 
> z = __import__('x.y.z', tail=True)  # or submodule in your initial post
> 
> I think that it is unintuitive that __import__(x.y.z) returns module x 
> instead of z. When I say "get me the book inside the box inside the 
> wardrobe", I expect to get the book, not the entire wardrobe! So even 
> though I disagree with all your criticisms of the three-line idiom, I 
> think there is an elegance to your idea that the three-line idiom 
> doesn't have. I'm +0.5 on the idea of an extra argument that says, "no, 
> really, just give me the book, I don't need the wardrobe".

May I point you to the two leading underscores? The name '__import__' 
clearly suggests that the function is part of Python's internals. By 
definition all attributes of the form __*__ are not meant to be used 
directly. Any suggestion to change the arguments of __import__() are 
futile. It's not going to happen unless the feature is required by 
Python's internal import system.

However I agree that something should be done here. I suggest to add a 
function to the imp module that does what you are trying to archive with 
tail=True.

Christian



More information about the Python-Dev mailing list