python-internals

python garbage collector behavior on compound objects

霸气de小男生 提交于 2019-11-28 01:01:20
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? 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 does this; the GC only deals in breaking circular references. This simple case is handled entirely by

How is the __name__ variable in a Python module defined?

微笑、不失礼 提交于 2019-11-27 21:11:40
I'm aware of the standard example : if you execute a module directly then it's __name__ global variable is defined as "__main__" . However, nowhere in the documentation can I find a precise description of how __name__ is defined in the general case. The module documentation says... Within a module, the module's name (as a string) is available as the value of the global variable __name__ . ...but what does it mean by "the module's name"? Is it just the name of the module (the filename with .py removed), or does it include the fully-qualified package name as well? How is the value of the __name_

How does Python's cmp_to_key function work?

自古美人都是妖i 提交于 2019-11-27 20:25:02
I came across this function here . I am baffled as to how this would be implemented -- how does the key function generated by cmp_to_key know what "position" a given element should be without checking how the given element compares with every other element of interest? The cmp_to_key method returns a special object that acts as a surrogate key: class K(object): __slots__ = ['obj'] def __init__(self, obj, *args): self.obj = obj def __lt__(self, other): return mycmp(self.obj, other.obj) < 0 def __gt__(self, other): return mycmp(self.obj, other.obj) > 0 def __eq__(self, other): return mycmp(self

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

北城以北 提交于 2019-11-27 20:21:55
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 tuple(), list() is list(), dict() is dict() (True, False, False) The only relevant piece of information

How does Python's cmp_to_key function work?

社会主义新天地 提交于 2019-11-27 19:10:06
问题 I came across this function here. I am baffled as to how this would be implemented -- how does the key function generated by cmp_to_key know what "position" a given element should be without checking how the given element compares with every other element of interest? 回答1: The cmp_to_key method returns a special object that acts as a surrogate key: class K(object): __slots__ = ['obj'] def __init__(self, obj, *args): self.obj = obj def __lt__(self, other): return mycmp(self.obj, other.obj) < 0

Is it possible to “hack” Python's print function?

柔情痞子 提交于 2019-11-27 16:56:22
Note: This question is for informational purposes only. I am interested to see how deep into Python's internals it is possible to go with this. Not very long ago, a discussion began inside a certain question regarding whether the strings passed to print statements could be modified after/during the call to print has been made. For example, consider the function: def print_something(): print('This cat was scared.') Now, when print is run, then the output to the terminal should display: This dog was scared. Notice the word "cat" has been replaced by the word "dog". Something somewhere somehow

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

只愿长相守 提交于 2019-11-27 16:16:19
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 integer sequences? Although there are a lot of questions in SO about hash and its order,but no one of them

Identifier normalization: Why is the micro sign converted into the Greek letter mu?

China☆狼群 提交于 2019-11-27 15:26:39
I just stumbled upon the following odd situation: >>> class Test: µ = 'foo' >>> Test.µ 'foo' >>> getattr(Test, 'µ') Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> getattr(Test, 'µ') AttributeError: type object 'Test' has no attribute 'µ' >>> 'µ'.encode(), dir(Test)[-1].encode() (b'\xc2\xb5', b'\xce\xbc') The character I entered is always the µ sign on the keyboard, but for some reason it gets converted. Why does this happen? There are two different characters involved here. One is the MICRO SIGN , which is the one on the keyboard, and the other is GREEK SMALL LETTER

Why do ints require three times as much memory in Python?

坚强是说给别人听的谎言 提交于 2019-11-27 14:58:40
On a 64-bit system an integer in Python takes 24 bytes. This is 3 times the memory that would be needed in e.g. C for a 64-bit integer. Now, I know this is because Python integers are objects. But what is the extra memory used for? I have my guesses, but it would be nice to know for sure. Remember that the Python int type does not have a limited range like C int has; the only limit is the available memory. Memory goes to storing the value, the current size of the integer storage (the storage size is variable to support arbitrary sizes), and the standard Python object bookkeeping (a reference

python is operator behaviour with string

混江龙づ霸主 提交于 2019-11-27 14:49:37
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:53) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> a = 'string' >>> b = a >>> b is a True >>> b =