使用纯Python函数,您可以按顺序(例如foo(1, 2, 3))或按名称(例如foo(a=1, c=3, b=2))传递参数.
C模块中定义的函数可以使用任一约定.你不能说range(stop=10, step=2),因此大多数但不是所有使用C接口实现的功能都是如此.
有没有办法确定从Python中传递函数约定的参数?
所以,我通常非常了解Python中的全局解释器锁(GIL)是如何工作的.本质上,当解释器正在运行时,一个线程将GIL保存为N个刻度(N可以使用设置sys.setcheckinterval),此时GIL被释放而另一个线程可以获取GIL.如果一个线程开始I/O操作,也会发生这种情况.
我有点困惑的是这一切是如何与C扩展模块一起工作的.
如果你有一个C扩展模块获取GIL,然后使用执行一些python代码PyEval_EvalCode,解释器是否可以释放GIL并将其提供给其他线程?或者获取GIL的C线程是否会永久保留GIL,直到PyEval_EvalCode返回并且GIL在C中明确释放?
PyGILState gstate = PyGILState_Ensure();
....
/* Can calling PyEval_EvalCode release the GIL and let another thread acquire it?? */
PyObject* obj = PyEval_EvalCode(code, global_dict, local_dict);
PyGILState_Release(gstate);
Run Code Online (Sandbox Code Playgroud) 当我们使用时,我们的代码需要10分钟来虹吸68,000条记录:
new_file = new_file + line + string
Run Code Online (Sandbox Code Playgroud)
但是,当我们执行以下操作时,只需1秒钟:
new_file += line + string
Run Code Online (Sandbox Code Playgroud)
这是代码:
for line in content:
import time
import cmdbre
fname = "STAGE050.csv"
regions = cmdbre.regions
start_time = time.time()
with open(fname) as f:
content = f.readlines()
new_file_content = ""
new_file = open("CMDB_STAGE060.csv", "w")
row_region = ""
i = 0
for line in content:
if (i==0):
new_file_content = line.strip() + "~region" + "\n"
else:
country = line.split("~")[13]
try:
row_region = regions[country]
except KeyError:
row_region = "Undetermined"
new_file_content += …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚Python 3(使用CPython作为解释器)如何执行其程序.我发现步骤是:
通过CPython编译器将Python源代码(.py文件)编译为Python字节码(.pyc)文件.在导入任何模块的情况下,保存.pyc文件,如果运行一个main.py Python脚本,则不保存它们.
Python虚拟机将字节码解释为硬件特定的机器码.
在这里找到一个很好的答案/sf/answers/121266841/说,与JVM相比,Python虚拟机运行其字节码所需的时间更长,因为java字节码包含有关数据类型的信息,而Python虚拟机解释了一行一个并且必须确定数据类型.
我的问题是Python虚拟机如何确定数据类型并在解释到机器代码期间或在单独的进程(例如会生成另一个中间代码)期间发生?
我正在写一篇关于 Pythonlist.clear()方法的博文,其中我还想提到底层算法的时间和空间复杂度。我预计时间复杂度为O(N),迭代元素并释放内存?但是,我发现一篇文章提到它实际上是一个O(1)操作。然后,我在 CPython 实现中搜索了该方法的源代码,找到了一个我认为是 的实际内部实现的方法list.clear(),但是,我不确定它是。下面是该方法的源代码:
static int
_list_clear(PyListObject *a)
{
Py_ssize_t i;
PyObject **item = a->ob_item;
if (item != NULL) {
/* Because XDECREF can recursively invoke operations on
this list, we make it empty first. */
i = Py_SIZE(a);
Py_SIZE(a) = 0;
a->ob_item = NULL;
a->allocated = 0;
while (--i >= 0) {
Py_XDECREF(item[i]);
}
PyMem_FREE(item);
}
/* Never fails; the return value can be ignored.
Note that there …Run Code Online (Sandbox Code Playgroud) 我正在阅读CPython中集合操作的时间复杂度,并了解到集合运算in符的平均时间复杂度为 O(1),最坏情况下的时间复杂度为 O(n)。我还了解到最坏的情况不会发生在 CPython 中,除非集合的哈希表的负载因子太高。
这让我想知道,在 CPython 实现中什么时候会发生这种情况?是否有一个简单的演示代码,它显示了一个具有明显可见的 O(n) 时间复杂度的in运算符的集合?
我正在研究 PEP 393 之后 Python 如何表示字符串,但我不明白 PyASCIIObject 和 PyCompactUnicodeObject 之间的区别。
我的理解是字符串用以下结构表示:
typedef struct {
PyObject_HEAD
Py_ssize_t length; /* Number of code points in the string */
Py_hash_t hash; /* Hash value; -1 if not set */
struct {
unsigned int interned:2;
unsigned int kind:3;
unsigned int compact:1;
unsigned int ascii:1;
unsigned int ready:1;
unsigned int :24;
} state;
wchar_t *wstr; /* wchar_t representation (null-terminated) */
} PyASCIIObject;
typedef struct {
PyASCIIObject _base;
Py_ssize_t utf8_length;
char *utf8;
Py_ssize_t wstr_length;
} PyCompactUnicodeObject; …Run Code Online (Sandbox Code Playgroud) Python 3.8 引入了PEP 578——Python 运行时审计挂钩,它承诺“使 Python 运行时采取的操作对审计工具可见”。
此 pep 提供了一些用例,这些用例似乎主要与系统管理员、安全专业人员和测试框架贡献者相关。然而,据我所知,这需要在选择的 python 实现上进行一些自定义选项。但是,我无法在网上找到太多信息来记录如何设置这些选项或哪些选项可用于在哪个实现中实现。
我如何开始在 cpython 中使用这些钩子?当前可用的所有钩子的完整列表在哪里(不建议实施)?
我正在经历python 语法规范并找到以下语句
\n\nfor_stmt:\n | \'for\' star_targets \'in\' ~ star_expressions \':\' [TYPE_COMMENT] 块 [else_block]\n\n
~这个语法规则是什么意思?语法中使用的其他符号(如&, !, |)已记录但尚未记录~。
\n\n\n
EBNF该符号是和的混合PEG。特别是,&后面跟有符号、标记或括号组表示正向先行(即需要匹配但不消耗),而!表示负向先行(即不需要匹配)。我们\n使用|分隔符来表示 PEG\xe2\x80\x99s \xe2\x80\x9cordered choice\xe2\x80\x9d (写为/传统 PEG 语法编写)
在 CPython 3.11 中,以下代码返回某些对象的非常大的引用计数。它似乎遵循预缓存的对象,例如整数 -5 到 256,但 CPython 3.10 不遵循:
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in (-6, -5, 0, 255, 256, 257):
... print(i, sys.getrefcount(i))
...
-6 5
-5 1000000004
0 1000000535
255 1000000010
256 1000000040
257 5
Run Code Online (Sandbox Code Playgroud)
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" …Run Code Online (Sandbox Code Playgroud) cpython ×10
python ×10
python-3.x ×2
string ×2
c ×1
ebnf ×1
in-operator ×1
parsing ×1
peg ×1
pep ×1
python-3.11 ×1
python-c-api ×1
set ×1