[Python-ideas] New use for the 'in' keyword.

Chris Angelico rosuav at gmail.com
Sun Mar 10 18:09:22 EDT 2019


On Mon, Mar 11, 2019 at 9:02 AM Simon <simon.bordeyne at gmail.com> wrote:
>
> Python's 'in' keyword has already several use cases, whether it's for testing inclusion in a set, or to iterate over that set, nevertheless, I think we could add one more function to that keyword.
>
> It's not uncommon to see star imports in some sources. The reason that people use star imports are mainly the following :
>
> - ease of use of the module's function
> - overwriting parts of a module with another module's similarly named functions.
>
> Obviously, there are plenty of cases where you don't want your module's functions to be overwritten, but sometimes, you do. I'm suggesting that the in keyword would be used to import a module in another module's namespace, with the following syntax :
>
> import numpy as np
> import math in np
>
> This has two advantages over star imports :
>
> 1/ You keep a namespace. For maintainability reasons, it's best to use namespaces
> 2/ You can overwrite parts of a module with another one
> 3/ IDEs would know which modules have been imported and which functions are defined there, which they can't have with star imports.

I'm not entirely sure what the effect of the second statement would
be. Is it like doing "np.sin = math.sin; np.sqrt = math.sqrt" for
every name in the math module? That seems like a massive amount of
monkeypatching. Or is it simply "np.math = math", so you could then
write "np.math.sqrt"? Less problematic, but also not all that useful.
Or is it something else?

Clarify please?

ChrisA


More information about the Python-ideas mailing list