Why can't I import from a module alias?

后端 未结 4 650
栀梦
栀梦 2020-11-30 12:09

I just stumbled across this unexpected behavior in python (both 2.7 and 3.x):

>>> import re as regexp
>>> regexp


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 12:52

    To get a definite answer on this you'll have to ask the designers themselves but, I think you're asking the wrong question.

    The question shouldn't be: Why is it done this way?" but, it should be, what would be the benefit of doing it the way you're asking? Surely it can be done but why should it?

    As is the import statement is dead simple and very intuitive, you give it a file name, it tries to finds load it up. You even get fancy as and from but, the concept is simply, you write filenames and you let it be.

    What would obfuscating it and making it harder understand achieve, the only achievement is making things arguably more complex.

    Python has a history of looking for the rationale behind changes to its design, people asking why aren't function objects subclassable will get a "Why should they?" reply; this behavior doesn't really have a use-case. As is, the import is simple, intuitive and reminiscent of including/using files in other languages.

提交回复
热议问题