python-internals

What does “del sys.modules[module]” actually do?

≯℡__Kan透↙ 提交于 2019-11-29 07:41:13
As everyone knows, you can do del sys.modules[module] to delete an imported module. So I was thinking: how is this different from rewriting sys.modules ? An interesting fact is, rewriting sys.modules can't truely delete a module. # a_module.py print("a module imported") Then import sys def func1(): import a_module # del sys.modules['a_module'] sys.modules = { k: v for k, v in sys.modules.items() if 'a_module' not in k} print('a_module' not in sys.modules) # True def func2(): import a_module func1() # a module imported func2() # no output here If I use del sys.modules['a_module'] , calling

Why does Python handle '1 is 1**2' differently from '1000 is 10**3'?

五迷三道 提交于 2019-11-29 05:28:01
Inspired by this question about caching small integers and strings I discovered the following behavior which I don't understand. >>> 1000 is 10**3 False I thought I understood this behavior: 1000 is to big to be cached. 1000 and 10**3 point to 2 different objects. But I had it wrong: >>> 1000 is 1000 True So, maybe Python treats calculations differently from 'normal' integers. But that assumption is also not correct: >>> 1 is 1**2 True How can this behavior be explained? Martijn Pieters There are two separate things going on here: Python stores int literals (and other literals) as constants

Making an object x such that “x in [x]” returns False

自闭症网瘾萝莉.ら 提交于 2019-11-29 05:20:45
问题 If we make a pathological potato like this: >>> class Potato: ... def __eq__(self, other): ... return False ... def __hash__(self): ... return random.randint(1, 10000) ... >>> p = Potato() >>> p == p False We can break sets and dicts this way ( note: it's the same even if __eq__ returns True , it's mucking with the hash that broke them): >>> p in {p} False >>> p in {p: 0} False Also len({p: 0, p: 0}) == 2 , and {p: 0}[p] raises KeyError, basically all mapping related stuff goes out the window

python 3.5 type hints: can i check if function arguments match type hints?

好久不见. 提交于 2019-11-29 04:22:31
does python 3.5 provide functions that allow to test whether a given argument would fit the type hints given in the function declaration? if i have e.g. this function: def f(name: List[str]): pass is there a python method that can check whether name = ['a', 'b'] name = [0, 1] name = [] name = None ... fit the type hints? i know that 'no type checking happens at runtime' but can i still check the validity of these arguments by hand in python? or if python does not provide that functionality itself: what is the tool i'd need to use? Ilya Peterov Python itself doesn't provide such functions, you

Runtime of python's if substring in string

社会主义新天地 提交于 2019-11-29 03:58:23
What is the big O of the following if statement ? if "pl" in "apple": ... What is the overall big O of how python determines if the string "pl" is found in the string "apple" or any other substring in string search. Is this the most efficient way to test if a substring is in a string? Does it use the same algorithm as .find() ? In python 3.4.2 it looks like they are resorting to the same function, but there may be difference in timing nevertheless. For example s.find first is required to lookup the find method of the string and such. The algorithm used is a mix between Boyer-More and Horspool.

Accessing the name that an object being created is assigned to

泪湿孤枕 提交于 2019-11-29 02:24:28
I'm writing some code to determine the name that an object is assigned to. This is for general debugging work and to further familiarize myself with python internals. I have it structured as a class decorator so that all instances of that class will have their names recorded if it is possible to do. The code is fairly long so I won't post it unless asked. The general technique is as follows though decorate the class' __init__ method with the code to do what I want set caller = inspect.currentframe().f_back and open inspect.getframeinfo(caller).filename and send it to ast.parse . I don't do any

How does __slots__ avoid a dictionary lookup?

微笑、不失礼 提交于 2019-11-29 01:39:23
I've heard that __slots__ makes objects faster by avoiding a dictionary lookup. My confusion comes from Python being a dynamic language. In a static language, we avoid a dictionary lookup for a.test by doing a compile-time optimisation to save the index in the instruction we run. Now, in Python, a could just as easily be another object that has a dictionary or a different set of attributes. It seems like we'll still have to do a dictionary lookup - the only difference seems to be that we only need one dictionary for the class, rather than a dictionary for each object. With this rational, How

Why is deque implemented as a linked list instead of a circular array?

与世无争的帅哥 提交于 2019-11-29 00:42:31
问题 CPython deque is implemented as a doubly-linked list of 64-item sized "blocks" (arrays). The blocks are all full, except for the ones at either end of the linked list. IIUC, the blocks are freed when a pop / popleft removes the last item in the block; they are allocated when append / appendleft attempts to add a new item and the relevant block is full. I understand the listed advantages of using a linked list of blocks rather than a linked list of items: reduce memory cost of pointers to prev

Why is string's startswith slower than in?

走远了吗. 提交于 2019-11-28 23:32:39
问题 Surprisingly, I find startswith is slower than in : In [10]: s="ABCD"*10 In [11]: %timeit s.startswith("XYZ") 1000000 loops, best of 3: 307 ns per loop In [12]: %timeit "XYZ" in s 10000000 loops, best of 3: 81.7 ns per loop As we all know, the in operation needs to search the whole string and startswith just needs to check the first few characters, so startswith should be more efficient. When s is big enough, startswith is faster: In [13]: s="ABCD"*200 In [14]: %timeit s.startswith("XYZ")

How print statement create a local variables

ⅰ亾dé卋堺 提交于 2019-11-28 21:15:42
Question are at the end of this post. First snippet: empty local variable dictionary. def outer(): x = 1 def inner(): print "Local variables: %s" % locals() return inner() print outer() Output: Local variables: {} Second snippet: print inside inner() function and creating local variable entry. def outer(): x = 1 def inner(): print x print "Local variables: %s" % locals() return inner() print outer() Output: 1 Local variables: {'x': 1} Third Snippet : del x from inside the inner function: def outer(): x = 1 def inner(): print x print "Local variables: %s" % locals() del x return inner() print