[Python-ideas] "maybe import"?

Amber Yust amber.yust at gmail.com
Sat Dec 28 20:14:28 CET 2013


I'm fairly convinced by the "too small" reasoning (summed up well by Nick).
I could potentially see a small and not incredibly intrusive improvement of
allowing imports from a tuple-style group of alternatives being useful for
things like c-accelerators, e.g...

    import (accelerated_foo, alternative_foo) as foo
    from (accelerated_foo, alternative_foo) import bar

But that would be a "nice to have" thing rather than a big difference.

On Sat Dec 28 2013 at 8:10:47 AM, Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Dec 29, 2013 at 1:59 AM, spir <denis.spir at gmail.com> wrote:
> >> This would also be allowed:
> >>
> >>      from this or that or another import spam
> >
> >
> > We'd also, or rather, need:
> >
> >         from this import foo or that import bar or another import baz as
> > spam
> >
> > which starts to be a bit complicated... Maybe (or perhaps) with parens:
> >
> >         from ((this import foo) or (that import bar) or (another import
> > baz)) as spam
>
> Can't say I like that variant - getting complicated and hard to read,
> even with parens. If you're importing different names from different
> modules, it's going to get very long, and then people are going to
> want to split it across lines, and then the obvious question is:
> what's been gained over the try/except variant?
>
> There is one thing, though, that I'm seeing of all this. Exception
> throwing is asymmetrical: you can attempt a series of statements until
> one fails, but there's no convenient syntax to attempt a series of
> statements until one succeeds. I wonder, could the more general case
> be solved? Is there a way to, without stupid stuff like eval, wrap up
> a few statements so they can be executed in a loop:
>
> def import_any(statement_list):
>     for try_me in statement_list:
>         try:
>             # uhh, this is the bit I'm not sure about...
>             try_me() # this would work if they're functions instead!
>             return
>         except ImportError:
>             pass
>     raise ImportError
>
> This works for a set of functions, but not for a bunch of "from this
> import that" statements. Would it be worth mangling the top of your
> script until it can be done with importlib or __import__? Doesn't seem
> nearly as clean, somehow.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131228/03df5e41/attachment.html>


More information about the Python-ideas mailing list