Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __import__() Magic Method

#1
Python __import__() Magic Method

🛑 Overriding this function is strongly discouraged. To change the semantics of the import statement, use import hooks instead!

Still here? 😉 So, let’s get started learning the syntax of this function. You can also check out our in-depth article on the __import__ statement here.

Syntax


 __import__(name, globals=None, locals=None, fromlist=(), level=0)

Parameter Description
name Import the module with this name.
globals Set the global namespace in which to search the name with this globals argument.
locals Set the local namespace in which to search the name with this locals argument.
fromlist Import the names of objects to be imported using this list argument.
level Use absolute (level=0) or relative imports (level>0), i.e., the number of parent directories to search relative to the calling module’s directory.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Examples Overriding __import__


The statement import my_module is semantically similar to the following:

my_module = __import__('my_module', globals(), locals(), [], 0)

In case you need some background:

  • Python globals() returns a dictionary of name --> object mappings. The names are the ones defined globally, i.e., defined by Python or in the outside scope of your program. The objects are the values associated to these names.
  • Python locals() returns a dictionary of name --> object mappings. The names are the ones defined in the current local scope, i.e., defined within the current module, class, method, or function—whatever the most local scope is.

But what if you import an individual name (e.g., xyz) from the module my_module? The call import my_module.xyz gets translated to code similar to this one:

my_module = __import__('my_module.xyz', globals(), locals(), [], 0)

💡 Note: The return value of the __import__() dunder method is the top-level module, not the name xyz within it!

However, to import a module by name, the more Pythonic approach is importlib.import_module().

References:

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2021/12/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
News - GTA Trilogy Physical Switch Versi...
Last Post: xSicKxBot
Today 11:24 AM
» Replies: 0
» Views: 32
News - Video: We Have A Lot Of Feelings ...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 49
News - Iconic Rockstar And Actor Meat Lo...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 13
(Indie Deal) FREE Last Horizon, Microids...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 18
Microsoft - Celebrating 20 years of Trus...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 13
Xbox Wire - New EA Play Member Rewards F...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 13
News - PS5 Will Outsell Series X|S 2:1 I...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 14
[Tut] Pandas get_dummies() – A Simple Gu...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 25
Mobile - Genshin Impact’s Tomo – vision,...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 14
News - Community Focus: Parris
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 17

[-]
Twitter



Discord Server © SickGaming.net 2012-2021