When is __all__ used while importing a module in Python?

0

Issue

Say there is a greetings module like this:

__all__ = []

def offer(func):
   __all__.append(func.__name__)
   return func

@offer
def spanish():
   return "Hola!"

@offer
def japanese():
   return "Konnichiwa"

When does the interpreter decide what to import when from greetings import * is run?

Solution

When does the interpreter decide what to import when from greetings
import * is run?

When you say from greetings import *, interpreter loads and executes the greeting module, then it references the objects mentioned in the __all__ list back into the current module’s global namespace so that you can access them using those symbols inside the __all__.

I think you guessed because you defined __all__ at the beginning of the module and it’s empty, nothing is going to be imported. No that’s not the case.

Answered By – SorousH Bakhtiary

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More