cpython

Why (0-6) is -6 = False? [duplicate]

拟墨画扇 提交于 2019-11-26 03:36:31
Possible Duplicate: Python “is” operator behaves unexpectedly with integers Today I tried to debug my project and after a few hours of analysing I'd got this: >>> (0-6) is -6 False but, >>> (0-5) is -5 True Could you explain to me, why? Maybe this is some kind of bug or very strange behavior. > Python 2.7.3 (default, Apr 24 2012, 00:00:54) [GCC 4.7.0 20120414 (prerelease)] on linux2 >>> type(0-6) <type 'int'> >>> type(-6) <type 'int'> >>> type((0-6) is -6) <type 'bool'> >>> kennytm All integers from -5 to 256 inclusive are cached as global objects sharing the same address with CPython, thus

Python vs Cpython

坚强是说给别人听的谎言 提交于 2019-11-26 02:59:31
问题 What\'s all this fuss about Python and CPython (Jython,IronPython) , I don\'t get it: python.org mentions that CPython is: The \"traditional\" implementation of Python (nicknamed CPython) yet another Stack Overflow question mentions that: CPython is the default byte-code interpreter of Python, which is written in C. Honestly I don\'t get what both of those explanations practically mean but what I thought was that, if I use CPython does that mean when I run a sample python code, it compiles it

Why does id({}) == id({}) and id([]) == id([]) in CPython?

醉酒当歌 提交于 2019-11-26 01:23:22
问题 Why does CPython (no clue about other Python implementations) have the following behavior? tuple1 = () tuple2 = () dict1 = {} dict2 = {} list1 = [] list2 = [] # makes sense, tuples are immutable assert(id(tuple1) == id(tuple2)) # also makes sense dicts are mutable assert(id(dict1) != id(dict2)) # lists are mutable too assert(id(list1) != id(list2)) assert(id(()) == id(())) # why no assertion error on this? assert(id({}) == id({})) # or this? assert(id([]) == id([])) I have a few ideas why it

Why (0-6) is -6 = False? [duplicate]

北战南征 提交于 2019-11-26 01:13:59
问题 Possible Duplicate: Python “is” operator behaves unexpectedly with integers Today I tried to debug my project and after a few hours of analysing I\'d got this: >>> (0-6) is -6 False but, >>> (0-5) is -5 True Could you explain to me, why? Maybe this is some kind of bug or very strange behavior. > Python 2.7.3 (default, Apr 24 2012, 00:00:54) [GCC 4.7.0 20120414 (prerelease)] on linux2 >>> type(0-6) <type \'int\'> >>> type(-6) <type \'int\'> >>> type((0-6) is -6) <type \'bool\'> >>> 回答1: All

How is set() implemented?

孤者浪人 提交于 2019-11-26 00:36:43
问题 I\'ve seen people say that set objects in python have O(1) membership-checking. How are they implemented internally to allow this? What sort of data structure does it use? What other implications does that implementation have? Every answer here was really enlightening, but I can only accept one, so I\'ll go with the closest answer to my original question. Thanks all for the info! 回答1: According to this thread: Indeed, CPython's sets are implemented as something like dictionaries with dummy

Why does Python code run faster in a function?

*爱你&永不变心* 提交于 2019-11-25 23:35:30
问题 def main(): for i in xrange(10**8): pass main() This piece of code in Python runs in (Note: The timing is done with the time function in BASH in Linux.) real 0m1.841s user 0m1.828s sys 0m0.012s However, if the for loop isn\'t placed within a function, for i in xrange(10**8): pass then it runs for a much longer time: real 0m4.543s user 0m4.524s sys 0m0.012s Why is this? 回答1: You might ask why it is faster to store local variables than globals. This is a CPython implementation detail. Remember