cpython

Why shouldn't I use PyPy over CPython if PyPy is 6.3 times faster?

非 Y 不嫁゛ 提交于 2019-11-26 14:57:26
问题 I've been hearing a lot about the PyPy project. They claim it is 6.3 times faster than the CPython interpreter on their site. Whenever we talk about dynamic languages like Python, speed is one of the top issues. To solve this, they say PyPy is 6.3 times faster. The second issue is parallelism, the infamous Global Interpreter Lock (GIL). For this, PyPy says it can give GIL-less Python. If PyPy can solve these great challenges, what are its weaknesses that are preventing wider adoption? That is

Using NumPy and Cpython with Jython

佐手、 提交于 2019-11-26 11:14:46
问题 I must use a commercial Java library, and would like to do it from Python. Jython is robust and I am fine with it being a few dot releases behind. However, I would like to use NumPy as well, which obviously does not work with Jython. Options like CPype and Java numeric libraries are unappealing. The former is essentially dead. The latter are mostly immature and lack the ease of use and wide acceptance of NumPy. My question is: How can one have Jython and Python code interoperate? It would be

How is tuple implemented in CPython?

五迷三道 提交于 2019-11-26 11:02:06
问题 I\'ve been trying to learn how CPython is implemented under the scenes. It\'s great that Python is high level, but I don\'t like treating it like a black box. With that in mind, how are tuples implemented? I\'ve had a look at the source (tupleobject.c), but it\'s going over my head. I see that PyTuple_MAXSAVESIZE = 20 and PyTuple_MAXFREELIST = 2000 , what is saving and the \"free list\"? (Will there be a performance difference between tuples of length 20/21 or 2000/2001? What enforces the

Are sets ordered like dicts in python3.6

让人想犯罪 __ 提交于 2019-11-26 09:41:26
问题 Due to changes in dict implementation in Python 3.6 it is now ordered by default. Do set s preserve order as well now? I could not find any information about it but as both of those data structures are very similar in the way they work under the hood I thought it might be the case. I know there is no promise for dict s to be ordered in all cases but they are most of the time. As stated in Python docs: The order-preserving aspect of this new implementation is considered an implementation

Python string with space and without space at the end and immutability

偶尔善良 提交于 2019-11-26 06:48:00
问题 I learnt that in some immutable classes, __new__ may return an existing instance - this is what the int , str and tuple types sometimes do for small values. But why do the following two snippets differ in the behavior? With a space at the end: >>> a = \'string \' >>> b = \'string \' >>> a is b False Without a space: >>> c = \'string\' >>> d = \'string\' >>> c is d True Why does the space bring the difference? 回答1: This is a quirk of how the CPython implementation chooses to cache string

Why do -1 and -2 both hash to -2 in CPython? [duplicate]

寵の児 提交于 2019-11-26 04:51:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: When is a python object's hash computed and why is the hash of -1 different? Why do -1 and -2 both hash to the same number if Python? Since they do, how does Python tell these two numbers apart? >>> -1 is -2 False >>> hash(-1) is hash(-2) True >>> hash(-1) -2 >>> hash(-2) -2 回答1: -1 is a reserved value at the C level of CPython which prevents hash functions from being able to produce a hash value of -1 . As

How to load a C# dll in python?

南笙酒味 提交于 2019-11-26 04:45:27
问题 how can I load a c# dll in python? Do I have to put some extra code in the c# files? (like export in c++ files) I don\'t want to use IronPython. I want to import a module to Python! 回答1: This is to answer the second part of your Question Try making the DLL COM visible. by using the [ComVisible(true)] Ok IronPython is a .net implemenatation of the Python language The technology is going to use the DLR of the .net 4.0 when it arrives so IronPython will have more Dynamism (is that a word). (In

Print to standard printer from Python?

拥有回忆 提交于 2019-11-26 04:44:24
问题 Is there a reasonably standard and cross platform way to print text (or even PS/PDF) to the system defined printer? Assuming CPython here, not something clever like using Jython and the Java printing API. 回答1: Unfortunately, there is no standard way to print using Python on all platforms. So you'll need to write your own wrapper function to print. You need to detect the OS your program is running on, then: For Linux - import subprocess lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess

list() uses slightly more memory than list comprehension

送分小仙女□ 提交于 2019-11-26 04:43:08
问题 So i was playing with list objects and found little strange thing that if list is created with list() it uses more memory, than list comprehension? I\'m using Python 3.5.2 In [1]: import sys In [2]: a = list(range(100)) In [3]: sys.getsizeof(a) Out[3]: 1008 In [4]: b = [i for i in range(100)] In [5]: sys.getsizeof(b) Out[5]: 912 In [6]: type(a) == type(b) Out[6]: True In [7]: a == b Out[7]: True In [8]: sys.getsizeof(list(b)) Out[8]: 1008 From the docs: Lists may be constructed in several

Python string 'in' operator implementation algorithm and time complexity

余生长醉 提交于 2019-11-26 03:43:39
问题 I am thinking of how the in operator implement, for instance >>> s1 = \'abcdef\' >>> s2 = \'bcd\' >>> s2 in s1 True In CPython, which algorithm is used to implement the string match, and what is the time complexity? Is there any official document or wiki about this? 回答1: It's a combination of Boyer-Moore and Horspool. You can view the C code here: Fast search/count implementation, based on a mix between Boyer-Moore and Horspool, with a few more bells and whistles on the top. For some more