python-internals

Why there is no difference between shallow copy and deep copy for a list of immutables

自闭症网瘾萝莉.ら 提交于 2019-12-11 02:24:59
问题 Suppose i have a python list l consisting of immutables. When i am doing a shallow copy and a deep copy , the result is same: >>> a = (1,2) # immutable types >>> b = (3,4) >>> l = [a,b] # a list containing immutable types >>> import copy >>> y = copy.copy(l) # shallow copy >>> z = copy.deepcopy(l) # deep copy >>> id(l[0]) 139857440375584 >>> id(y[0]) 139857440375584 >>> id(z[0]) 139857440375584 # all have the same id's , so all refer to the same object Does it means that shallow copy and deep

How is sorted(key=lambda x:) implemented behind the scene?

微笑、不失礼 提交于 2019-12-10 23:10:57
问题 An example: names = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison"] sorted(names, key=lambda name: name.split()[-1].lower()) I know key is used to compare different names, but it can have two different implementations: First compute all keys for each name, and bind the key and name together in some way, and sort them. The p Compute the key each time when a comparison happens The problem with the first approach is that it has to define another data structure to bind

Where does a Python list hold its values?

左心房为你撑大大i 提交于 2019-12-10 19:51:56
问题 From introspection perspective, where list values are located in the class object. If list object is class in python: >>> a = ['one', 'two'] >>> type(a) <class 'list'> So it is stored somewhere in the class, but where? For example: If we define a class with values: class Test: def __init__(self): self.test_name = "Do Not" self.test_surname = "Know" It is easy to locate an instance values: >>> b = Test() >>> print(a.__dict__) {'test_surname': 'Know', 'test_name': 'Do not'} Is there similar

Python bytecode function call passing self

我们两清 提交于 2019-12-10 19:28:04
问题 I'm trying to understand how bytecode works. a.func() is a function call. The corresponding bytecode is roughly LOAD_GLOBAL a , LOAD_ATTR attr and then CALL_FUNCTION with 0 arguments. This is totally fine if a is a module. But if a is an object, it has to pass the object instance itself. Since Python could NOT know whether a is a module or an object at compile time, naturally the bytecode is same regardless of the type of a . But how does the runtime system handle self as the first argument

Python: equality for Nan in a list?

核能气质少年 提交于 2019-12-10 17:15:05
问题 I just want to figure out the logic behind these results: >>>nan = float('nan') >>>nan == nan False # I understand that this is because the __eq__ method is defined this way >>>nan in [nan] True # This is because the __contains__ method for list is defined to compare the identity first then the content? But in both cases I think behind the scene the function PyObject_RichCompareBool is called right? Why there is a difference? Shouldn't they have the same behaviour? 回答1: But in both cases I

Does converting from bytearray to bytes incur a copy?

本小妞迷上赌 提交于 2019-12-10 12:56:37
问题 Does converting from the mutable bytearray type to the non-mutable bytes type incur a copy? Is there any cost associated with it, or does the interpreter just treat it as an immutable byte sequence, like casting a char* to a const char* const in C++? ba = bytearray() ba.extend("some big long string".encode('utf-8')) # Is this conversion free or expensive? write_bytes(bytes(ba)) Does this differ between Python 3 where bytes is its own type and Python 2.7 where bytes is just an alias for str ?

When does CPython garbage collect?

微笑、不失礼 提交于 2019-12-10 10:41:14
问题 If my understanding is correct, in CPython objects will be deleted as soon as their reference count reaches zero. If you have reference cycles that become unreachable that logic will not work, but on occasion the interpreter will try to find them and delete them (and you can do this manually by calling gc.collect() ). My question is, when do these interpreter-triggered cycle collection steps happen? What kind of events trigger them? I am more interested in the CPython case, but would love to

Python string concatenation internal details

廉价感情. 提交于 2019-12-09 13:01:58
问题 Assume that we have a list of strings and we want to create a string by concatenating all element in this list. Something like this: def foo(str_lst): result = '' for element in str_lst: result += element return result Since strings are immutable objects, I expect that python creates a new str object and copy contents of result and element at each iteration. It makes O(M * N^2) time complexity, M is the length of each element and N is the size of the list. However, my experiment shows that it

In Python, what is the difference between f.readlines() and list(f)

断了今生、忘了曾经 提交于 2019-12-09 09:41:29
问题 From both Python2 Tutorial and Python3 Tutorial, there is a line in the midpoint of section 7.2.1 saying: If you want to read all the lines of a file in a list you can also use list(f) or f.readlines(). So my question is: What is the difference between these two ways to turn a file object to a list? I am curious both in performance aspect and in underneath Python object implementation (and maybe the difference between the Python2 and Python3). 回答1: Functionally, there is no difference; both

When to use `<>` and `!=` operators?

戏子无情 提交于 2019-12-09 03:48:16
问题 Couldn't find much on this. Trying to compare 2 values, but they can't be equal. In my case, they can be (and often are) either greater than or less than. Should I use: if a <> b: dostuff or if a != b: dostuff This page says they're similar, which implies there's at least something different about them. 回答1: Quoting from Python language reference, The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent. So, they both are