[Python-ideas] "maybe import"?
Bruce Leban
bruce at leapyear.org
Fri Dec 27 04:39:38 CET 2013
On Thu, Dec 26, 2013 at 5:57 PM, Amber Yust <amber.yust at gmail.com> wrote:
> Where the behavior would essentially be as above - attempt to run the
> import normally, and in cases where the import fails, map the name to a
> value of None instead. Users who want a different behavior are still free
> to use the long-form syntax. A possibly variant might be to also only run
> the import if the name isn't already bound,
>
I think you mean do the import if the name is unbound *or bound to
None.* Otherwise,
it doesn't work in the example you gave.
Here's another use case. Some modules, e.g., pycrypto need to be compiled
in order to be installed. I have a stub version of pycrypto which can be
used as a substitute for testing without actually encrypting anything.
Rather than using a new keyword, the try keyword would fit here fine:
try import Crypto
try import CryptoStub as Crypto
note that there are cases where this can't be a drop in replacement:
try import Crypto.Random
try import CryptoStub.Random as Crypto.Random # not allowed
you'd have to instead write:
try import Crypto.Random as CryptoRandom
try import CryptoStub.Random as CryptoRandom
--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131226/7d9628b6/attachment.html>
More information about the Python-ideas
mailing list