__import__

Ryan Paul segphault at sbcglobal.net
Sat May 8 04:19:25 EDT 2004


On Sat, 08 May 2004 09:40:39 +0200, Heiko Wundram wrote:

> Am Samstag, 8. Mai 2004 09:31 schrieb Coder Coder:
>> Can someone help me with how to overload the __import__ function,
>> so that I can call the old __import__ function and if it cannot find
>> the library to be able to do something else.
> 
> You shouldn't overload a builtin (at least if you expect to be compatible with 
> other programmers libraries), but rather do the following where you need it:
> 
> try:
> 	import <my-package>
> except ImportError:
> 	<do something else>
> 
> Repeat after me: "Overloading a builtin is evil! Overloading a builtin is 
> evil!"
> 
> HTH!
> 
> Heiko.

nothing evil about it, as long as you just EXTEND the functionality rather
than replace it. If an overloaded import still behaves the way the rest of
the libraries expect it to, you dont break anything. If there was a risk
of dangerous compatibility issues, there wouldn't be a hook designed to
facilitate overloading it. Repeat after me: "Its ok to use a language
feature as long as I know what I am doing." ;-D



More information about the Python-list mailing list