On Thu, Dec 26, 2013 at 5:57 PM, Amber Yust <amber.yust@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@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.

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/



--
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.