[Python-ideas] "maybe import"?
Mathias Panzenböck
grosser.meister.morti at gmx.net
Fri Dec 27 18:31:07 CET 2013
I often do this:
try:
import json
except ImportError:
import simplejson as json
Or:
try:
import optional_feature
except ImportError:
HAS_OPTIONAL_FEATURE = False
else:
HAS_OPTIONAL_FEATURE = True
I think these two cases are fairly common. So why not have something like this?:
import json or simplejson as json
On 12/27/2013 06:00 PM, David Mertz wrote:
> On Thu, Dec 26, 2013 at 5:57 PM, Amber Yust <amber.yust at gmail.com <mailto:amber.yust at gmail.com>> wrote:
>
> It's a fairly standard pattern to see things like this:
>
> try:
> import foo
> except ImportError:
> foo = None
>
>
> I would rarely, if ever, follow this pattern, although I *would* do conditional import fairly often. E.g. these are
> things I might do (well, not really these specific ones, but just as pattern examples):
>
> try:
> from math import pi
> except ImportError:
> pi = 3.1415
>
> Or:
>
> try:
> import fancymodule as mod
> except ImportError:
> import simpler_version as mod
>
> Or:
>
> try:
> from mymod import needed_func
> except ImportError:
> def needed_func(a,b,c):
> "Bare bones implementation of more general function"
> return a*b + c
>
> Or even:
>
> try:
> import cool_feature
> FEATURE_AVAIL = True
> except ImportError:
> FEATURE_AVAIL = False
>
> What these have in common is that they are each specific to the context, and actually have nothing much in common other
> than the possibility import might fail. The special case of defining a missing module as 'None' is something I will
> probably never want to do... and therefore definitely have no desire for special syntax to do it.
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons. Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>
>
> On Fri, Dec 27, 2013 at 8:30 AM, David Mertz <mertz at gnosis.cx <mailto:mertz at gnosis.cx>> wrote:
>
> It's a fairly standard pattern to see things like this:
>
> try:
> import foo
> except ImportError:
> foo = None
>
> (and of course, variants with from...import et cetera). These can potentially add a lot of clutter to the imports
> section of a file, given that it requires 4 lines to do a conditional import.
>
> It seems like it'd be useful and clean to have a syntax that looked like this:
>
> maybe import foo
> from bar maybe import baz
> from qux maybe import quy as quz
>
> 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, so that
> you could do something like...
>
> from frobber_a maybe import frob as frobber
> from frobbler_b maybe import frobble as frobber
> from frobber_c maybe import frobit as frobber
>
> ...to potentially try different fallback options if the first choice for an interface provider isn't available.
>
More information about the Python-ideas
mailing list