cpython

Python: Lifetime of module-global variables

空扰寡人 提交于 2020-01-23 00:57:07
问题 I have a shared resource with high initialisation cost and thus I want to access it across the system (it's used for some instrumentation basically, so has to be light weight). So I created a module managing the setup and access to it. It does a lazy initialise of the resource and stores it in a module global variable. I then use functions of this module across the system to operate on the resource. - Now I am wondering whether (or how often) I will have to reinitialise the resource? - I know

Why does naive string concatenation become quadratic above a certain length?

我是研究僧i 提交于 2020-01-22 07:21:27
问题 Building a string through repeated string concatenation is an anti-pattern, but I'm still curious why its performance switches from linear to quadratic after string length exceeds approximately 10 ** 6: # this will take time linear in n with the optimization # and quadratic time without the optimization import time start = time.perf_counter() s = '' for i in range(n): s += 'a' total_time = time.perf_counter() - start time_per_iteration = total_time / n For example, on my machine (Windows 10,

getting the C python exec argument string or accessing the evaluation stack

ε祈祈猫儿з 提交于 2020-01-13 06:34:49
问题 In my python debugger I have a way of remapping a string to a filename so that when you are stepping through an exec'd function inside the debugger you can list lines pygmentized, or view them along inside an editor like Emacs via realgud. So I'd like to be able to extract the string in an exec statement when CPython is stopped inside evaluating that. I already have a mechanism that can look back in the call frame to see if the caller was an EXEC_STMT and I can look back one instruction to

Python source code for built-in “in” operator

核能气质少年 提交于 2020-01-09 18:38:31
问题 I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this implementation? My goal is to improve the sub-string search in Python by extending different C implementations of this search, although I am not sure if Python already uses the idea I have. 回答1: To find the implementation of any python operator, first

Python source code for built-in “in” operator

雨燕双飞 提交于 2020-01-09 18:37:05
问题 I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this implementation? My goal is to improve the sub-string search in Python by extending different C implementations of this search, although I am not sure if Python already uses the idea I have. 回答1: To find the implementation of any python operator, first

Python source code for built-in “in” operator

余生长醉 提交于 2020-01-09 18:35:14
问题 I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this implementation? My goal is to improve the sub-string search in Python by extending different C implementations of this search, although I am not sure if Python already uses the idea I have. 回答1: To find the implementation of any python operator, first

Python 解释器 Nuitka

好久不见. 提交于 2020-01-07 09:19:34
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 原文来自:https://www.oschina.net/p/nuitka 前言 Nuitka是一个 Python 的替代 编译器 。它可以无缝地替代和扩展 Python 的解释和编译工作。现在支持CPython2.6、2..7、3.2、3.3和3.4版本。它可以执行编译的代码,并能用很兼容的方式将目标代码一起编译。 开发者可以自由的使用所有的Python模块库和其他全部的第三方扩展库。Nuitka可以将 Python 代码编译成C级别的程序,并像CPython调用libpython一样去使用libpython库去执行编译后的程序。Nuitka的这些的优化工作旨在是为了能够避免Python执行过程中的在某个环节产生的不必要的开销。并旨在能够是消除一些不兼容性,虽然这是一种改进了的模式,但也不是每个标准 Python 的bug都能模拟,例如,提供更完整的错误信息。 Nuitka 某些地方比 CPython 要更快些,不过目前还没有进行全面的性能优化,发展空间巨大。 不需要对环境变量进行修改,可以不改变环境的情况下在脚本目录以外直接运行nuitka和nuitka-run。作为一个可选项,为了方便也可以将bin目录加入到PATH中。 Nuitka和其它unix程序一样具有—help参数选项来获得帮助: 1nuitka

How to find the cpython source code file for a specific 3rd party (NumPy for instance) API?

亡梦爱人 提交于 2020-01-06 04:30:06
问题 This question comes from this post. I am digging out the underlying logic for numpy.linalg.eig . w, v = np.linalg.eig(C.T*C) v matrix([[-0.9486833 , -0.31622777], [ 0.31622777, -0.9486833 ]]) the implementation of np.linalg.eig w, vt = _umath_linalg.eig(a, signature=signature, extobj=extobj) is linked here, which links to _umath_linalg.py _umath_linalg.py corresponds to <python3.6>/site-packages/numpy/linalg/_umath_linalg.cpython-36m-darwin.so I searched lots of keywords comes from _umath

How to map func_closure entries to variable names?

允我心安 提交于 2020-01-03 08:45:25
问题 I have a lambda object that is created in this function: def add_url_rule(self, rule, endpoint=None, view_func=None, **options): self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options)) Using func_closure of the lambda function object I can access the closure scope of the lambda function: (<cell at 0x3eb89f0: str object at 0x2fb4378>, <cell at 0x3eb8a28: function object at 0x3cb3a28>, <cell at 0x3eb8a60: str object at 0x3ebd090>, <cell at 0x3eb8b08: dict object at 0x3016ec0

What is the stack in Python?

依然范特西╮ 提交于 2020-01-02 08:22:12
问题 What do we call "stack" in Python? Is it the C stack of CPython? I read that Python stackframes are allocated in a heap. But I thought the goal of a stack was... to stack stackframes. What does the stack do then? 回答1: Oversimplifying slightly: In CPython, when PyEval_EvalFrameEx is evaluating a Python stack frame's code, and comes to a direct function call, it allocates a new Python stack frame, links it up… and then recursively calls PyEval_EvalFrameEx on that new frame. So, the C stack is a