New use for the 'in' keyword.

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. Additionnally, The following syntax would also be valid : import math import numpy in math

On Mon, Mar 11, 2019 at 9:02 AM Simon <simon.bordeyne@gmail.com> wrote:
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

Hi Simon You've suggested allowing import numpy as np import math in np as an improvement on from somewhere import * But you can get a similar result already, by writing in your package import .aaa as np and within mypackage/aaa.py writing from numpy import * from math import * When you write import numpy as np the value of the identifier becomes the numpy module. Your proposal aims to add to that module additional key-value pairs. (A module is a bit like a dict.) And perhaps clobber some existing resources. It's generally best to treat a module as a read-only object. That way, you don't get side-effects. This is why Chris was talking about monkey-patching. -- Jonathan

On Mon, Mar 11, 2019 at 9:02 AM Simon <simon.bordeyne@gmail.com> wrote:
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

Hi Simon You've suggested allowing import numpy as np import math in np as an improvement on from somewhere import * But you can get a similar result already, by writing in your package import .aaa as np and within mypackage/aaa.py writing from numpy import * from math import * When you write import numpy as np the value of the identifier becomes the numpy module. Your proposal aims to add to that module additional key-value pairs. (A module is a bit like a dict.) And perhaps clobber some existing resources. It's generally best to treat a module as a read-only object. That way, you don't get side-effects. This is why Chris was talking about monkey-patching. -- Jonathan
participants (3)
-
Chris Angelico
-
Jonathan Fine
-
Simon