python-internals

Python: the __getattribute__ method and descriptors

瘦欲@ 提交于 2019-11-30 16:25:28
according to this guide on python descriptors https://docs.python.org/2/howto/descriptor.html method objects in new style classes are implemented using descriptors in order to avoid special casing them in attribute lookup. the way I understand this is that there is a method object type that implements __get__ and returns a bound method object when called with an instance and an unbound method object when called with no instance and only a class. the article also states that this logic is implemented in the object.__getattribute__ method. like so: def __getattribute__(self, key): "Emulate type

Meaning of unittest.main() in Python unittest module

醉酒当歌 提交于 2019-11-30 11:57:32
问题 I was trying to learn unit testing in Python, specifically the unittest module. Consider the following lines: import unittest class abc(unittest.TestCase): def xyz(): ... if __name__ == "__main__": unittest.main() I could see all my test cases running because of the call to unittest.main() . I was just curious to know how this call is making all the test cases run. I know since I'm inheriting from unittest.TestCase for every test class, it is doing all the magic. Any insights? 回答1: main

Why are dict lookups always better than list lookups?

我是研究僧i 提交于 2019-11-30 11:45:49
问题 I was using a dictionary as a lookup table but I started to wonder if a list would be better for my application -- the amount of entries in my lookup table wasn't that big. I know lists use C arrays under the hood which made me conclude that lookup in a list with just a few items would be better than in a dictionary (accessing a few elements in an array is faster than computing a hash). I decided to profile the alternatives but the results surprised me. List lookup was only better with a

scope of eval function in python

拥有回忆 提交于 2019-11-30 11:04:34
Consider the following example: i=7 j=8 k=10 def test(): i=1 j=2 k=3 return dict((name,eval(name)) for name in ['i','j','k']) It returns: >>> test() {'i': 7, 'k': 10, 'j': 8} Why eval does not take into consideration the variables defined inside the function? From the documentation, optionally you can pass a globals and a locals dictionary. What does it means?Finally, how can I modify this small case to make it work? Generators are implemented as function scopes : The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this

Why `float` function is slower than multiplying by 1.0?

无人久伴 提交于 2019-11-30 11:01:48
I understand that this could be argued as a non-issue, but I write software for HPC environments, so this 3.5x speed increase actually makes a difference. In [1]: %timeit 10 / float(98765) 1000000 loops, best of 3: 313 ns per loop In [2]: %timeit 10 / (98765 * 1.0) 10000000 loops, best of 3: 80.6 ns per loop I used dis to have a look at the code, and I assume float() will be slower as it requires a function call (unfortunately I couldn't dis.dis(float) to see what it's actually doing). I guess a second question would be when should I use float(n) and when should I use n * 1.0 ? thefourtheye

Why is copying a shuffled list much slower?

帅比萌擦擦* 提交于 2019-11-30 10:19:51
问题 Copying a shuffled range(10**6) list ten times takes me about 0.18 seconds: (these are five runs) 0.175597017661 0.173731403198 0.178601711594 0.180330912952 0.180811964451 Copying the unshuffled list ten times takes me about 0.05 seconds: 0.058402235973 0.0505464636856 0.0509734306934 0.0526022752744 0.0513324916184 Here's my testing code: from timeit import timeit import random a = range(10**6) random.shuffle(a) # Remove this for the second test. a = list(a) # Just an attempt to "normalize"

globals() vs locals() mutability

孤人 提交于 2019-11-30 10:19:21
In Python, globals() returns a representation of the global symbol table, while locals() returns a representation of the local state. While both return a dictionary, changes to globals() are effected in the global symbol table, while change to locals() have no effect. Why is this the case? Function locals are highly optimised and determined at compile time, CPython builds on not being able to alter the known locals dynamically at runtime. You can see this when decoding a function bytecode: >>> import dis >>> def foo(): ... a = 'bar' ... return a + 'baz' ... >>> dis.dis(foo) 2 0 LOAD_CONST 1 (

Why must Python list addition be homogenous?

守給你的承諾、 提交于 2019-11-30 08:05:10
Can anyone familiar with Python's internals (CPython, or other implementations) explain why list addition is required to be homogenous: In [1]: x = [1] In [2]: x+"foo" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\Users\Marcin\<ipython-input-2-94cd84126ddc> in <module>() ----> 1 x+"foo" TypeError: can only concatenate list (not "str") to list In [3]: x+="foo" In [4]: x Out[4]: [1, 'f', 'o', 'o'] Why shouldn't the x+"foo" above return the same value as the final value of x in the above transcript? This question follows

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

烂漫一生 提交于 2019-11-30 07:47:52
问题 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? 回答1: There are two

Where is Python's shutdown procedure documented?

不羁岁月 提交于 2019-11-30 05:35:27
CPython has a strange behaviour where it sets modules to None during shutdown. This screws up error logging during shutdown of some multithreading code I've written. I can't find any documentation of this behaviour. It's mentioned in passing in PEP 432 : [...] significantly reducing the number of modules that will experience the "module globals set to None" behaviour that is used to deliberate break cycles and attempt to releases more external resources cleanly. There are SO questions about this behaviour and the C API documentation mentions shutdown behaviour for embedded interpreters . I've