Does this:
import foo as bar
do the same thing as this?
bar = __import__('foo')
Is there ever a reason to use the latter one?
I'm reading through someone elses code, I found the latter one and am not sure why they didn't use the prior syntax
Direct use of
__import__()
is rare, except in cases where you want to import a module whose name is only known at runtime.
source
The two statements do the same thing. The only reason to use the latter syntax is not knowing the module name in advance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With