python-internals

What is the stack in Python?

依然范特西╮ 提交于 2020-01-02 08:22:12
问题 What do we call "stack" in Python? Is it the C stack of CPython? I read that Python stackframes are allocated in a heap. But I thought the goal of a stack was... to stack stackframes. What does the stack do then? 回答1: Oversimplifying slightly: In CPython, when PyEval_EvalFrameEx is evaluating a Python stack frame's code, and comes to a direct function call, it allocates a new Python stack frame, links it up… and then recursively calls PyEval_EvalFrameEx on that new frame. So, the C stack is a

List lookup faster than tuple?

可紊 提交于 2020-01-01 23:53:11
问题 In the past, when I've needed array-like indexical lookups in a tight loop, I usually use tuples, since they seem to be generally extremely performant (close to using just n-number of variables). However, I decided to question that assumption today and came up with some surprising results: In [102]: l = range(1000) In [103]: t = tuple(range(1000)) In [107]: timeit(lambda : l[500], number = 10000000) Out[107]: 2.465047836303711 In [108]: timeit(lambda : t[500], number = 10000000) Out[108]: 2

difference between python set and dict “internally”

南笙酒味 提交于 2020-01-01 18:48:16
问题 Can anybody tell me how the internal implementation of set and dict is different in python? Do they use the same data structure in the background? ++ In theory, one can use dict to achieve set functionality. 回答1: In CPython, sets and dicts use the same basic data structure. Sets tune it slightly differently, but it is basically a hash table just like dictionaries. You can take a look at the implementation details in the C code: setobject.c and dictobject.c; the implementations are very close;

difference between python set and dict “internally”

旧时模样 提交于 2020-01-01 18:48:06
问题 Can anybody tell me how the internal implementation of set and dict is different in python? Do they use the same data structure in the background? ++ In theory, one can use dict to achieve set functionality. 回答1: In CPython, sets and dicts use the same basic data structure. Sets tune it slightly differently, but it is basically a hash table just like dictionaries. You can take a look at the implementation details in the C code: setobject.c and dictobject.c; the implementations are very close;

What is the difference between type.__getattribute__ and object.__getattribute__?

守給你的承諾、 提交于 2020-01-01 05:55:04
问题 Given: In [37]: class A: ....: f = 1 ....: In [38]: class B(A): ....: pass ....: In [39]: getattr(B, 'f') Out[39]: 1 Okay, that either calls super or crawls the mro? In [40]: getattr(A, 'f') Out[40]: 1 This is expected. In [41]: object.__getattribute__(A, 'f') Out[41]: 1 In [42]: object.__getattribute__(B, 'f') --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-42-de76df798d1d> in <module>() ----> 1

What is the difference between type.__getattribute__ and object.__getattribute__?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 05:54:06
问题 Given: In [37]: class A: ....: f = 1 ....: In [38]: class B(A): ....: pass ....: In [39]: getattr(B, 'f') Out[39]: 1 Okay, that either calls super or crawls the mro? In [40]: getattr(A, 'f') Out[40]: 1 This is expected. In [41]: object.__getattribute__(A, 'f') Out[41]: 1 In [42]: object.__getattribute__(B, 'f') --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-42-de76df798d1d> in <module>() ----> 1

Python: which types support weak references?

老子叫甜甜 提交于 2020-01-01 04:23:05
问题 Code: from weakref import WeakSet se = WeakSet() se.add(1) Output: TypeError: cannot create weak reference to 'int' object Doc: Several built-in types such as list and dict do not directly support weak references but can add support through subclassing: ... Other built-in types such as tuple and int do not support weak references even when subclassed (This is an implementation detail and may be different across various Python implementations.). This isn't expressive enough to explain: Why

Why is __code__ for a function(Python) mutable

孤者浪人 提交于 2020-01-01 04:05:50
问题 In a previous question yesterday, in comments, I came to know that in python __code__ atrribute of a function is mutable. Hence I can write code as following def foo(): print "Hello" def foo2(): print "Hello 2" foo() foo.__code__ = foo2.__code__ foo() Output Hello Hello 2 I tried googling, but either because there is no information(I highly doubt this), or the keyword ( __code__ ) is not easily searchable, I couldn't find a use case for this. It doesn't seem like "because most things in

Why is __code__ for a function(Python) mutable

拥有回忆 提交于 2020-01-01 04:05:06
问题 In a previous question yesterday, in comments, I came to know that in python __code__ atrribute of a function is mutable. Hence I can write code as following def foo(): print "Hello" def foo2(): print "Hello 2" foo() foo.__code__ = foo2.__code__ foo() Output Hello Hello 2 I tried googling, but either because there is no information(I highly doubt this), or the keyword ( __code__ ) is not easily searchable, I couldn't find a use case for this. It doesn't seem like "because most things in

Why does a class definition always produce the same bytecode?

*爱你&永不变心* 提交于 2020-01-01 02:04:06
问题 Say I do: #!/usr/bin/env python # encoding: utf-8 class A(object): pass Now I disassemble it: python -m dis test0.py 4 0 LOAD_CONST 0 ('A') 3 LOAD_NAME 0 (object) 6 BUILD_TUPLE 1 9 LOAD_CONST 1 (<code object A at 0x1004ebb30, file "test0.py", line 4>) 12 MAKE_FUNCTION 0 15 CALL_FUNCTION 0 18 BUILD_CLASS 19 STORE_NAME 1 (A) 22 LOAD_CONST 2 (None) 25 RETURN_VALUE Now I add some statements in the class definition: #!/usr/bin/env python # encoding: utf-8 class A(object): print 'hello' 1+1 pass