Issue
When I was trying to figure out the use of imp.load_module in Python, I got the following code (origin page). It's my first time seeing the use of * in Python, is that some pointer like things?
thanks in advance
import imp
import dbgp
info = imp.find_module(modname, dbgp.__path__)
_client = imp.load_module(modname, *info)
sys.modules["_client"] = _client
from _client import *
del sys.modules["_client"], info, _client
Solution
The *
on front if info
causes the list / tuple to be unwrapped into individual arguments to the function. You can read more about unpacking here in the python documentation. this can also be done with dictionaries for named arguments, see here
For example,
def do_something(a,b,c,d):
print("{0} {1} {2} {3}".format(a,b,c,d))
a = [1,2,3,4]
do_something(*a)
Output:
1 2 3 4
EDIT:
According to comments to your question by jcomeau_ictx, the operator is called splat
Answered By - GWW
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.