[Python-ideas] Make import an expression
Chris Angelico
rosuav at gmail.com
Sat Jul 14 06:05:48 EDT 2018
On Sat, Jul 14, 2018 at 7:39 PM, Jonathan Fine <jfine2358 at gmail.com> wrote:
> Hi Ken
>
> Thank you for your clear subject line. As you probably already know, in
> Python, assignments are not expressions, and so we can't write
> if (a=get()):
> # do something with a
> One reason for this is, perhaps, that "Explicit is better than implicit" (as
> in The Zen of Python, via "import this").
>
> Your last example of the intended semantics is
> b = a * (import math).tan(math.radians(45))
> which demonstrate that you intend the statement
> a = (import math)
> to have the side effect of assigning a value to the identifier 'math'.
>
> I'm concerned that your intended semantics are similar to "assignment as an
> expression", and so wouldn't fit in well with the rest of Python. More
> concretely, when reading a module I expect to see all the imports at the
> top, much like a recipe starts with a list of ingredients.
>
> It may be that the documentation regarding import and __import__ can be
> improved, and that there's a practical problem behind your experience that
> __import__ is cumbersome and takes space.
>
>>> import importlib
>>> secret = importlib.import_module('random').randint(1, 100)
>>> secret
83
Looks like you CAN import as an expression :)
ChrisA
More information about the Python-ideas
mailing list