cpython

How exactly is Python Bytecode Run in CPython?

偶尔善良 提交于 2019-12-29 02:34:44
问题 I am trying to understand how Python works (because I use it all the time!). To my understanding, when you run something like python script.py, the script is converted to bytecode and then the interpreter/VM/CPython–really just a C Program–reads in the python bytecode and executes the program accordingly. How is this bytecode read in? Is it similar to how a text file is read in C? I am unsure how the Python code is converted to machine code. Is it the case that the Python interpreter (the

Nested list comprehension scope

℡╲_俬逩灬. 提交于 2019-12-28 01:02:29
问题 The best way to explain my question is with an example: example.py: class A(object): integers = [1, 2, 3] singles = [i for i in integers] class B(object): integers = [1, 2, 3] pairs = [(i, j) for i in integers for j in integers] When I run this under python 2 it works fine, but under python 3 I get a NameError for class B (but not class A ): $ python example.py Traceback (most recent call last): File "example.py", line 6, in <module> class B(object): File "example.py", line 8, in B pairs = [

undefined symbol: PyOS_mystrnicmp

落爺英雄遲暮 提交于 2019-12-25 07:59:16
问题 I tried installing pysqlite , but I'm having some trouble using it. >>> import pysqlite2.dbapi2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/data/.pyenv/versions/2.7.5/lib/python2.7/site-packages/pysqlite2/dbapi2.py", line 28, in <module> from pysqlite2._sqlite import * ImportError: /data/.pyenv/versions/2.7.5/lib/python2.7/site-packages/pysqlite2/_sqlite.so: undefined symbol: PyOS_mystrnicmp I think I might be missing some Python headers. Where do I find them

How to create the int 1 at two different memory locations?

三世轮回 提交于 2019-12-25 02:43:44
问题 I want to show someone how using is instead of == to compare integers can fail. I thought this would work, but it didn't: >>> import copy >>> x = 1 >>> y = copy.deepcopy(x) >>> x is y True I can do this easily for bigger integers: >>> x = 500 >>> y = 500 >>> x is y False How can I demonstrate the same thing with smaller integers which might typically be used for enum-like purposes in python? 回答1: The following example fails in both Python 2 and 3: >>> n=12345 >>> ((n**8)+1) % (n**4) is 1

Python 2.X中的range和xrange函数之间有什么区别?

狂风中的少年 提交于 2019-12-24 17:27:50
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 显然,xrange更快,但是我不知道为什么它更快(到目前为止,除了轶事之外,还没有证据表明它更快)或除此之外还有什么不同 for i in range(0, 20): for i in xrange(0, 20): #1楼 根据扫描/打印0-N个项目的要求,range和xrange的工作方式如下。 range()-在内存中创建一个新列表,并将整个0到N个项目(总共N + 1个)打印出来。 xrange()-创建一个迭代器实例,该实例扫描项目并仅将当前遇到的项目保留在内存中,因此始终使用相同数量的内存。 如果所需元素只是在列表的开头,那么它可以节省大量时间和内存。 #2楼 我很震惊,没有人读 doc : 此函数与 range() 非常相似,但是返回一个 xrange 对象而不是列表。 这是一种不透明的序列类型,其产生的值与对应的列表相同,而实际上并没有同时存储它们。 xrange() 优于 range() 的优势很小(因为 xrange() 仍然需要在创建值时才创建值),除非在内存不足的计算机上使用了非常大的范围,或者所有范围的元素都在从未使用过(例如,当环路通常与终止 break )。 #3楼 什么? range 在运行时返回静态列表。 xrange 返回一个 object (该 object 行为类似于生成器

WCF with netTcpBinding + cPython

穿精又带淫゛_ 提交于 2019-12-24 15:33:02
问题 I've read this question WCF and Python. But in case, the wcf service use netTcpBinding, could we call it from cPython. If it's possible, please help to give an simple example ? 回答1: could we call it from cPython No, netTcpBinding is interoperable only with WCF clients. From here: The default configuration for the NetTcpBinding is faster than the configuration provided by the WSHttpBinding, but it is intended only for WCF-to-WCF communication . From comments: does wsHttpBinding work or only

Is an explicit NUL-byte necessary at the end of a bytearray for cython to be able to convert it to a null-terminated C-string

孤人 提交于 2019-12-24 08:57:14
问题 When converting a bytearray -object (or a bytes -object for that matter) to a C-string, the cython-documentation recommends to use the following: cdef char * cstr = py_bytearray there is no overhead, as cstr is pointing to the buffer of the bytearray -object. However, C-strings are null-terminated and thus in order to be able to pass cstr to a C-function it must also be null-terminated. The cython-documentation doesn't provide any information, whether the resulting C-strings are null

Python GIL prevent CPU usage to exceed 100% in multiple core machine?

懵懂的女人 提交于 2019-12-23 19:51:27
问题 Many references say that, Python GIL lower down the performance of multi threading code in multi core machine, since each thread will need to acquire the GIL before executioin. In other words, it looks like GIL make a multi threading Python program to a single thread mode in fact. For example: (1) Thread A get GIL, execute some time, release GIL (2) Thread B get GIL, execute some time, release GIL ... However, after some simple experiments, I found that although GIL lower down the performance

Calling a Python function from a C thread, with a mutable C array

筅森魡賤 提交于 2019-12-23 19:28:35
问题 I'm in the process of creating a Python extension for a small audio library written in C++. When opening an audio stream, a callback function is passed as a parameter (among other parameters of course). A sligthly simplified use case: AudioThingy *a = new AudioThingy(); a->openStream(..., callbackFunction); a->startStream(); My Python extension wraps this inside a Python class. thingy = AudioThingy() thingy.openStream(..., pythonCallbackFunction) thingy.startStream() Now, the extension has a

Ironpython: Function works in CPython, mysterious null pointer exception in IronPython

瘦欲@ 提交于 2019-12-23 15:39:58
问题 I'm trying to do something that seems very simple, and falls within the range of standard python. The following function takes a collection of sets, and returns all of the items that are contained in two or more sets. To do this, while the collection of sets is not empty, it simply pops one set out of the collection, intersects it with the remaining sets, and updates a set of items that fall in one of these intersections. def cross_intersections(sets): in_two = set() sets_copy = copy(sets)