python-internals

Why does '() is ()' return True when '[] is []' and '{} is {}' return False?

試著忘記壹切 提交于 2019-11-26 22:56:35
问题 From what I've been aware of, using [], {} or () to instantiate objects returns a new instance of list, dict or tuple respectively; a new instance object with a new identity . This was pretty clear to me until I actually tested it and I noticed that () is () actually returns True instead of the expected False : >>> () is (), [] is [], {} is {} (True, False, False) as expected, this behavior is also manifested when creating objects with list(), dict() and tuple() respectively: >>> tuple() is

Why is float() faster than int()?

廉价感情. 提交于 2019-11-26 22:52:45
问题 Experimenting with some code and doing some microbenchmarks I just found out that using the float function on a string containing an integer number is a factor 2 faster than using int on the same string. >>> python -m timeit int('1') 1000000 loops, best of 3: 0.548 usec per loop >>> python -m timeit float('1') 1000000 loops, best of 3: 0.273 usec per loop It gets even stranger when testing int(float('1')) which runtime is shorter than the bare int('1') . >>> python -m timeit int(float('1'))

python garbage collector behavior on compound objects

我怕爱的太早我们不能终老 提交于 2019-11-26 21:48:56
问题 Does python garbage collector cleans up a compound object if some of its parts are still referenced e.g. def foo(): A = [ [1, 3, 5, 7], [2, 4, 6, 8]] return A[1] B = foo() Will A[0] be garbage collected? Is there a way to confirm the same through code? 回答1: Nothing references the list A and the nested list A[0] , so yes, they will be deleted from memory. The nested list object referenced by A[1] has no connection back to its original container. Note that it's not the garbage collector that

Why is one class variable not defined in list comprehension but another is?

主宰稳场 提交于 2019-11-26 21:04:29
I just read the answer to this question: Accessing class variables from a list comprehension in the class definition It helps me to understand why the following code results in NameError: name 'x' is not defined : class A: x = 1 data = [0, 1, 2, 3] new_data = [i + x for i in data] print(new_data) The NameError occurs because x is not defined in the special scope for list comprehension. But I am unable to understand why the following code works without any error. class A: x = 1 data = [0, 1, 2, 3] new_data = [i for i in data] print(new_data) I get the output [0, 1, 2, 3] . But I was expecting

Two variables in Python have same id, but not lists or tuples

给你一囗甜甜゛ 提交于 2019-11-26 19:55:18
Two variables in Python have the same id : a = 10 b = 10 a is b >>> True If I take two list s: a = [1, 2, 3] b = [1, 2, 3] a is b >>> False according to this link Senderle answered that immutable object references have the same id and mutable objects like lists have different ids. So now according to his answer, tuples should have the same ids - meaning: a = (1, 2, 3) b = (1, 2, 3) a is b >>> False Ideally, as tuples are not mutable, it should return True , but it is returning False ! What is the explanation? Immutable objects don't have the same id , and as a mater of fact this is not true

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'

Why do tuples take less space in memory than lists?

两盒软妹~` 提交于 2019-11-26 18:58:55
问题 A tuple takes less memory space in Python: >>> a = (1,2,3) >>> a.__sizeof__() 48 whereas list s takes more memory space: >>> b = [1,2,3] >>> b.__sizeof__() 64 What happens internally on the Python memory management? 回答1: I assume you're using CPython and with 64bits (I got the same results on my CPython 2.7 64-bit). There could be differences in other Python implementations or if you have a 32bit Python. Regardless of the implementation, list s are variable-sized while tuple s are fixed-size.

What's the logic behind Python's hash function order?

佐手、 提交于 2019-11-26 18:37:33
问题 As we know, Some of Python's data structures use hash tables for storing items like set or dictionary . So there is no order in these objects. But it seems that, for some sequences of numbers that's not true. For example consider the following examples : >>> set([7,2,5,3,6]) set([2, 3, 5, 6, 7]) >>> set([4,5,3,0,1,2]) set([0, 1, 2, 3, 4, 5]) But it isn't sorted if we make a small change : >>> set([8,2,5,3,6]) set([8, 2, 3, 5, 6]) So the question is: How does Python's hash function work on

python is operator behaviour with string [duplicate]

别等时光非礼了梦想. 提交于 2019-11-26 18:24:34
问题 This question already has an answer here: About the changing id of an immutable string 5 answers I am unable to understand the following behaviour. I am creating 2 strings, and using is operator to compare it. On the first case, it is working differently. On the second case, it works as expected. What is the reason when I use comma or space, it is showing False on comparing with is and when no comma or space or other characters are used, it gives True Python 3.6.5 (default, Mar 30 2018, 06:41

time.sleep — sleeps thread or process?

五迷三道 提交于 2019-11-26 18:05:45
In Python for *nix, does time.sleep() block the thread or the process? It blocks the thread. If you look in Modules/timemodule.c in the Python source, you'll see that in the call to floatsleep() , the substantive part of the sleep operation is wrapped in a Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS block, allowing other threads to continue to execute while the current one sleeps. You can also test this with a simple python program: import time from threading import Thread class worker(Thread): def run(self): for x in xrange(0,11): print x time.sleep(1) class waiter(Thread): def run(self):