cpython

What happens behind the scenes when python adds small ints? [duplicate]

孤街浪徒 提交于 2019-11-26 21:08:51
问题 This question already has answers here : “is” operator behaves unexpectedly with integers (11 answers) Closed 4 years ago . I was fiddling around with id recently and realized that (c?)Python does something quite sensible: it ensures that small ints always have the same id . >>> a, b, c, d, e = 1, 2, 3, 4, 5 >>> f, g, h, i, j = 1, 2, 3, 4, 5 >>> [id(x) == id(y) for x, y in zip([a, b, c, d, e], [f, g, h, i, j])] [True, True, True, True, True] But then it occurred to me to wonder whether the

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

拜拜、爱过 提交于 2019-11-26 19:05:30
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? This is a quirk of how the CPython implementation chooses to cache string literals. String literals with the same contents may refer to the same string object, but they don't have to. 'string'

Comparing None with built-in types using arithmetic operators?

让人想犯罪 __ 提交于 2019-11-26 18:23:32
问题 Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> None > 0 False >>> None == 0 False >>> None < 0 True Is comparing None using arithmetic operators well defined for built-in types (integers in this case)? Is the difference between the first two and the third comparison part of language specification (Python's specification - you must be kidding :)) or is it CPython's implementation

How to load a C# dll in python?

只愿长相守 提交于 2019-11-26 17:36:24
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! Vivek Bernard 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 english if you're a Python guru, you'll feel more at home when you use IronPython) So you may

list() uses more memory than list comprehension

会有一股神秘感。 提交于 2019-11-26 17:23:18
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 ways: Using a pair of square brackets to denote the empty list: [] Using square brackets, separating items

Why does the `is` operator behave differently in a script vs the REPL?

二次信任 提交于 2019-11-26 17:17:41
问题 In python, two codes have different results: a = 300 b = 300 print (a==b) print (a is b) ## print True print ("id(a) = %d, id(b) = %d"%(id(a), id(b))) ## They have same address But in shell mode(interactive mode): >>> a = 300 >>> b = 300 >>> a is b False >>> id(a) 4501364368 >>> id(b) 4501362224 "is" operator has different results. 回答1: When you run code in a .py script, the entire file is compiled into a code object before executing it. In this case, CPython is able to make certain

How to generate a repeatable random number sequence?

你离开我真会死。 提交于 2019-11-26 16:59:36
问题 I would like a function that can generate a pseudo-random sequence of values, but for that sequence to be repeatable every run. The data I want has to be reasonably well randomly distributed over a given range, it doesn't have to be perfect. I want to write some code which will have performance tests run on it, based on random data. I would like that data to be the same for every test run, on every machine, but I don't want to have to ship the random data with the tests for storage reasons

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

淺唱寂寞╮ 提交于 2019-11-26 16:37:20
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 andrew cooke -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 noted by DSM, the same is not true in IronPython and PyPy where hash(-1) != hash(-2) . See this Quora answer : If you write a type in a C extension module and

Print to standard printer from Python?

怎甘沉沦 提交于 2019-11-26 16:11:19
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 . Anuj Gupta 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.PIPE) lpr.stdin.write(your_data_here) For Windows: http://timgolden.me.uk/python/win32_how_do_i

CPython is bytecode interpreter?

ⅰ亾dé卋堺 提交于 2019-11-26 16:09:50
问题 I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture? Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they are implemented in different languages). I also read somewhere that Python is C++ interpretation. Is this correct? And what does that mean? I'm still very new to Python, so forgive me if I