Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer in python?

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
like image 297
alwinlin Avatar asked Dec 10 '25 09:12

alwinlin


1 Answers

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

like image 132
GWW Avatar answered Dec 11 '25 22:12

GWW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!