cpython

Why is a False value (0) smaller in bytes than True (1)?

笑着哭i 提交于 2019-11-29 10:45:41
问题 I was playing around with sys 's getsizeof() and found that False (or 0 ) consists of less bytes than True (or 1 ). Why is that? import sys print("Zero: " + str(sys.getsizeof(0))) print("One: " + str(sys.getsizeof(1))) print("False: " + str(sys.getsizeof(False))) print("True: " + str(sys.getsizeof(True))) # Prints: # Zero: 24 # One: 28 # False: 24 # True: 28 In fact, other numbers (also some that consist of more than one digit) are 28 bytes. for n in range(0, 12): print(str(n) + ": " + str

Overriding the newline generation behaviour of Python's print statement

℡╲_俬逩灬. 提交于 2019-11-29 10:22:11
I have a bunch of legacy code for encoding raw emails that contains a lot of print statements such as print >>f, "Content-Type: text/plain" This is all well and good for emails, but we're now leveraging the same code for outputting HTTP request. The problem is that the Python print statement outputs '\n' whilst HTTP requires '\r\n' . It looks like Python (2.6.4 at least) generates a trailing PRINT_NEWLINE byte code for a print statement which is implemented as ceval.c:1582: err = PyFile_WriteString("\n", w); Thus it appears there's no easy way to override the default newline behaviour of print

Python 3.8测试阶段正式开始,发布Beta 1版

梦想与她 提交于 2019-11-29 07:25:01
上周,Python背后的团队宣布发布了Python 3.8.0b1 版本,这是Python 3.8计划的四个beta发行预览版中的第一个。此版本标志着beta阶段的开始,您可以在此阶段测试新特性,并使您的应用程序为新版本做好准备。 下面是即将发布的Python 3.8版本中的一些特性: 赋值表达式 在经过Python开发人员的广泛讨论之后,在PEP 572中提出了赋值表达式。这个特性引入了一个新的操作符(:=),您可以使用它在表达式中分配变量。 Positional-only参数 在Python中,可以通过位置、关键字或两者同时传递参数给函数。API设计人员有时可能希望仅限制按位置传递参数。为了方便实现这一点,Python 3.8将附带一个新标记(/),以指示其左边的参数仅是位置的。这类似于*,它指示右边的参数仅为关键字。 Python的初始化配置 Python是高度可配置的,但是配置分散在代码中。该版本为Python初始化C API引入了新的函数和结构,为Python开发人员提供了配置Python的“简单而可靠的方法”。 CPython的Vectorcall协议 增强功能引入了代码的灵活性和性能。为了优化对象的调用,本版本引入了Vectorcall协议和一个已在内部用于Python和内置函数的调用约定。 运行时审计钩子 Python 3.8将提供两个新的api: Audit

Why does this Python script run 4x slower on multiple cores than on a single core

别说谁变了你拦得住时间么 提交于 2019-11-29 07:03:48
I'm trying to understand how CPython's GIL works and what are the differences between GIL in CPython 2.7.x and CPython 3.4.x. I'm using this code for benchmarking: from __future__ import print_function import argparse import resource import sys import threading import time def countdown(n): while n > 0: n -= 1 def get_time(): stats = resource.getrusage(resource.RUSAGE_SELF) total_cpu_time = stats.ru_utime + stats.ru_stime return time.time(), total_cpu_time, stats.ru_utime, stats.ru_stime def get_time_diff(start_time, end_time): return tuple((end-start) for start, end in zip(start_time, end

Python 3.5 vs. 3.6 what made “map” slower compared to comprehensions

匆匆过客 提交于 2019-11-29 02:56:13
问题 I sometimes used map if there was a function/method that was written in C to get a bit extra performance. However I recently revisited some of my benchmarks and noticed that the relative performance (compared to a similar list comprehension) drastically changed between Python 3.5 and 3.6. That's not the actual code but just a minimal sample that illustrates the difference: import random lst = [random.randint(0, 10) for _ in range(100000)] assert list(map((5).__lt__, lst)) == [5 < i for i in

Migrating from CPython to Jython

微笑、不失礼 提交于 2019-11-29 02:28:26
问题 I'm considering moving my code (around 30K LOC) from CPython to Jython, so that I could have better integration with my java code. Is there a checklist or a guide I should look at, to help my with the migration? Does anyone have experience with doing something similar? From reading the Jython site, most of the problems seem too obscure to bother me. I did notice that: thread safety is an issue Unicode support seems to be quite different, which may be a problem for me mysqldb doesn't work and

Why is deque implemented as a linked list instead of a circular array?

与世无争的帅哥 提交于 2019-11-29 00:42:31
问题 CPython deque is implemented as a doubly-linked list of 64-item sized "blocks" (arrays). The blocks are all full, except for the ones at either end of the linked list. IIUC, the blocks are freed when a pop / popleft removes the last item in the block; they are allocated when append / appendleft attempts to add a new item and the relevant block is full. I understand the listed advantages of using a linked list of blocks rather than a linked list of items: reduce memory cost of pointers to prev

Why is string's startswith slower than in?

走远了吗. 提交于 2019-11-28 23:32:39
问题 Surprisingly, I find startswith is slower than in : In [10]: s="ABCD"*10 In [11]: %timeit s.startswith("XYZ") 1000000 loops, best of 3: 307 ns per loop In [12]: %timeit "XYZ" in s 10000000 loops, best of 3: 81.7 ns per loop As we all know, the in operation needs to search the whole string and startswith just needs to check the first few characters, so startswith should be more efficient. When s is big enough, startswith is faster: In [13]: s="ABCD"*200 In [14]: %timeit s.startswith("XYZ")

C Python: Running Python code within a context

不问归期 提交于 2019-11-28 21:27:45
The Python C API function PyEval_EvalCode let's you execute compiled Python code. I want to execute a block of Python code as if it were executing within the scope of a function , so that it has its own dictionary of local variables which don't affect the global state. This seems easy enough to do, since PyEval_EvalCode lets you provide a Global and Local dictionary: PyObject* PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) The problem I run into has to do with how Python looks up variable names. Consider the following code, that I execute with PyEval_EvalCode : myvar =

Python source code for built-in “in” operator

我与影子孤独终老i 提交于 2019-11-28 20:52:23
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. To find the implementation of any python operator, first find out what bytecode Python generates for it, using the dis.dis function : >>> dis.dis("'0' in ()") 1 0