[英]pointer in python?
当我试图弄清楚Python中 imp.load_module 的使用时,我得到了以下代码( 原始页面)。 Python 中 * 的用法我还是第一次见,是指针之类的东西吗?
提前致谢
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
我假设您正在谈论_client = imp.load_module(modname, *info)
行。
不,它不是指针。 它扩展了作为参数传入的列表。 这是一个例子:
In [7]: def foo(bar, baz):
...: return bar + baz
...:
In [8]: l = [1,2]
In [9]: foo(l)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/Daenyth/<ipython console> in <module>()
TypeError: foo() takes exactly 2 arguments (1 given)
In [10]: foo(*l)
Out[10]: 3
字典也有类似的扩展。
In [12]: d = {'bar': 1, 'baz': 2}
In [13]: foo(**d)
Out[13]: 3
本质上,它们是保存另一个变量的 memory 地址的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.