[Python-ideas] "maybe import"?
spir
denis.spir at gmail.com
Sat Dec 28 15:59:52 CET 2013
On 12/27/2013 05:08 AM, Steven D'Aprano wrote:
> Hmmm. The basic idea makes a certain level of sense to me, but I'm not
> sure it makes enough sense to overcome the barrier required before
> adding a new keyword.
Same for me. In fact, while the use case is clear and common, I'm not sure
syntax it's worth dedicated syntax at all, especially because this happens once
in a module and at a definite place (import section).
> [...]
>
> If we're entertaining changes to imports, another possibility would be
> to allow fallback module names:
>
> import this or that or another as this
I like that form.
> Each of "this", "that", "another" will be attempted, the first
> successful import being bound to the name "this". The "as this" part
> would be mandatory, so as to require a consistent name regardless of
> which module was imported. This would be a syntax error, since it isn't
> clear what name would be bound at the end:
>
> import this or that or another
Right.
> 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
?
> With this syntax, we could add None as a special case:
>
> import this or that or another or None as this
Yep!
> would be equivalent to:
>
> module_names = ("this", "that", "another", "None")
> for name in module_names:
> if name == "None":
> spam = None
> else:
> try:
> this = __import__(name)
> except ImportError:
> continue
> break
> else:
> raise ImportError
Seems correct, expected semantics.
> and the "from...import" case could be written as:
>
> from this or that or None import spam
>
>
> roughly equivalent to:
>
> module_names = ("this", "that", "another", "None")
> for name in module_names:
> if name == "None":
> spam = None
> else:
> try:
> temp = __import__(name)
> spam = temp.spam
> except ImportError:
> continue
> break
> else:
> raise ImportError
Right.
> Advantages:
>
> - covers both use-cases where you want to try a series of
> modules, and the one where you fall back to None;
Plus, as shown above we need to cover the case were a imported element have
various names in original module. (Either all cases are covered, or it's
definitely not worth bothering, imo.)
> - "or" is already a keyword, no new keywords needed;
>
> - reads more like English;
Not clearer than "maybe", in my ears (sic!) but I'm not english native speaker.
> - "import None" currently gives SyntaxError, so this
> can't interfere with modules actually called "None".
>
>
> Disadvantages:
>
> - more complexity to imports;
>
> - only saves a few lines;
>
> - this usage of "or" is not quite the same as the usage
> as a boolean operator, e.g. different from "x in a or b".
Yes, it's more like prioritized choice in parsing (an operand in either a symbol
or a real or an int), which is at times termed 'or' as well.
Denis
More information about the Python-ideas
mailing list