Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why does the _ _ import__ of Python need fromlist

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Why does Python's _ _ import__ need fromlist? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

In Python, if you want to import modules dynamically, you can do the following:

Module = _ _ import__ ('module_name')

If you want to import submodules, you might think this is a simple question:

Module = _ _ import__ ('module_name.submodule')

As it turns out, of course it doesn't work, you have to do this:

Module = _ _ import__ ('module_name.submodule', fromlist= [' xxx'])

Why? The actual value fromlist doesn't seem to matter at all, as long as it's not empty.

In fact, _ _ import__ is also implemented internally by import.

So when we use import, we generally have the following five ways:

Import pkg

Import pkg.mod

From pkg import mod, mod2

From pkg.mod import func, func2

From pkg.mod import submod

In the first two cases, the import statement assigns the leftmost module object to: pkg. Later import pkg.mod can use pkg.mod.func () in this way, because the import statement introduces the native module pkg, which is a module object with the mod attribute. So the _ _ import__ function must return the leftmost module object in order to assign it to pkg. These two situations are equivalent to

Pkg = _ _ import__ ('pkg')

Pkg = _ _ import__ ('pkg.mod')

In the last three cases, import must do more work, it must get and assign multiple names from the module object. But the _ _ import__ function can only return one object, and it cannot retrieve more than one name from the module object, so in the third case:

Pkg = _ _ import__ ('pkg')

Mod = pkg.mod

Mod2 = pkg.mod2

However, if mod and mod2 have not been imported in the module pkg, it will not take effect. The _ _ import__ function needs to know that mod and mod2 are the names it can access so that it can see if they are modules and try to import them. So like this, it's closer:

Pkg = _ _ import__ ('pkg', fromlist= [' mod', 'mod2'])

Mod = pkg.mod

Mod2 = pkg.mod2

This causes _ _ import__ to try pkg.mod and pkg.mod2 first, and it doesn't throw an exception if it doesn't work. But the fifth example still doesn't work in the above way:

Tmp = _ _ import__ ('pkg.mod', fromlist= [' submod'])

Submod = tmp.submod

We want tmp to be a pkg.mod module, but it's actually a pkg module. If it is loaded in the way we want it to be, more additional unpacking work will be added. So it returns directly to the rightmost module if and only if the fromlist is not empty.

The fromlist in the _ _ import__ function actually has no specific meaning, you can understand that it is just a tag, when it is not empty, import will import the rightmost module in the string written earlier for us. When it is empty, import will import the leftmost module of the string for us, that's all.

This is the answer to the question about why Python's _ _ import__ needs fromlist. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report