PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 27, 2022

[FIXED] When is __all__ used while importing a module in Python?

 November 27, 2022     module, package, python     No comments   

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 - S.B
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing