python-internals

When is del useful in python?

你说的曾经没有我的故事 提交于 2019-12-16 20:02:09
问题 I can't really think of any reason why python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign None to it. And when deleting from a dictionary, a del method could be added. Is there any reason to keep del in python, or is it a vestige of Python's pre-garbage collection days? 回答1: Firstly, you can del other things besides local variables del list_item[4] del dictionary["alpha"] Both of which

multiprocessing - Cancel remaining jobs in a pool without destroying the Pool

﹥>﹥吖頭↗ 提交于 2019-12-13 02:16:13
问题 I'm using map_async to create a pool of 4 workers. And giving it a list of image files to process [Set 1]. At times, I need to cancel the processing in between, so that I can instead get a different set of files processed [Set 2]. So an example situation is, I gave map_async 1000 files to process. And then want to cancel the processing of remaining jobs after about 200 files have been processed. Additionally, I want to do this cancellation without destroying/terminating the pool. Is this

Is there a way to check if function is recursive in python?

六月ゝ 毕业季﹏ 提交于 2019-12-12 15:06:49
问题 I want to write a testing function for an exercise, to make sure a function is implemented correctly. So I got to wonder, is there a way, given a function "foo", to check if it is implemented recursively? If it encapsulates a recursive function and uses it it also counts. For example: def foo(n): def inner(n): #more code inner(n-1) return inner(n) This should also be considered recursive. Note that I want to use an external test function to perform this check. Without altering the original

Puzzled with LOAD_FAST/STORE_FAST of python

我怕爱的太早我们不能终老 提交于 2019-12-12 12:32:23
问题 When I wrote some code, I found a interesting thing: def test(): l = [] for i in range(10): def f():pass print(f) #l.append(f) test() import dis dis.dis(test) The output is : <function test.<locals>.f at 0x7f46c0b0d400> <function test.<locals>.f at 0x7f46c0b0d488> <function test.<locals>.f at 0x7f46c0b0d400> <function test.<locals>.f at 0x7f46c0b0d488> <function test.<locals>.f at 0x7f46c0b0d400> <function test.<locals>.f at 0x7f46c0b0d488> <function test.<locals>.f at 0x7f46c0b0d400>

Python method accessor creates new objects on each access?

血红的双手。 提交于 2019-12-12 07:49:25
问题 When investigating for another question, I found the following: >>> class A: ... def m(self): return 42 ... >>> a = A() This was expected: >>> A.m == A.m True >>> a.m == a.m True But this I did not expect: >>> a.m is a.m False And especially not this: >>> A.m is A.m False Python seems to create new objects for each method access. Why am I seeing this behavior? I.e. what is the reason why it can't reuse one object per class and one per instance? 回答1: Yes, Python creates new method objects for

Best way to import modules in Python

a 夏天 提交于 2019-12-12 04:54:49
问题 Is there a performance disadvantage when importing all module functions into the namespace at once, as in: from numpy import * A = array([...]) compared to only importing the module function when you need to use it, as in: import numpy as np A = np.array([...]) 回答1: It won't have any noticeable effect on performance. It does pollute the module's namespace with a bunch of functions that may shadow built-ins (for example, numpy include its own sum implementation), and in general it makes it

Where builtin functions are implemented

强颜欢笑 提交于 2019-12-12 04:28:28
问题 I tried to look around but I couldn't find anything clear about this topic. Are built-in functions implemented in a module that is automatically imported every time Python is launched? In the case which is the module? Or are built-in functions just embedded functions inside the Python interpreter? 回答1: For CPython, the built-in functions are (for the most part) implemented in the bltinmodule.c file. The exceptions are mostly the types; things like str and dict and list have their own C files

Why does '20000 is 20000' result in True? [duplicate]

此生再无相见时 提交于 2019-12-12 01:29:38
问题 This question already has an answer here : What's with the integer cache maintained by the interpreter? (1 answer) Closed 3 years ago . is in Python tests if 2 references point to the same object. Numbers between -5 and 256 are cached internally so: a = 10 b = 10 a is b # Results in True How does this explain something like: 20000 is 20000 # Results in True Both numbers are above 256. Should not the 2 integers be 2 distinct objects? 回答1: The Python interpreter sees you are re-using a

Python - Why not all immutable objects are always cached?

こ雲淡風輕ζ 提交于 2019-12-11 06:55:12
问题 I am not sure what is happening under the hood with regards to the Python object model for the code below. You can download the data for the ctabus.csv file from this link import csv def read_as_dicts(filename): records = [] with open(filename) as f: rows = csv.reader(f) headers = next(rows) for row in rows: route = row[0] date = row[1] daytype = row[2] rides = int(row[3]) records.append({ 'route': route, 'date': date, 'daytype': daytype, 'rides': rides}) return records # read data from csv

Handling map function in python2 & python3

会有一股神秘感。 提交于 2019-12-11 06:07:30
问题 Recently i came across a question & confused with a possible solution, code part is // code part in result reader result = map(int, input()) // consumer call result_consumer(result) its not about how do they work, the problem is when you are running in python2 it will raise an exception, on result fetching part, so result reader can handle the exception, but incase of python3 a map object is returned, so only consumer will be able to handle exception. is there any solution keeping map