python-internals

What's the difference between a reversed tuple and a reversed list?

牧云@^-^@ 提交于 2019-12-09 00:24:07
问题 Reversing a tuple and reversing a list returns objects of different type: >>> reversed((1,2)) <reversed at 0x7fffe802f748> >>> reversed([1,2]) <list_reverseiterator at 0x7fffebdd4400> They have the same dir . Neither type is a subclass of the other. Why is that? What can one do that the other can't? 回答1: Basically, a list implements the __reversed__ method and returns an specialized object, while tuple falls back to the default implementation of reversed for any sequence: >>> list.__reversed_

Python's int function performance

佐手、 提交于 2019-12-08 17:39:40
问题 Does Python's built-in function int still try to convert the submitted value even if the value is already an integer? More concisely: is there any performance difference between int('42') and int(42) caused by conversion algorithm? 回答1: As per the comments in the source code, Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__ (). For floating point numbers, this truncates towards zero. If x is not a number or if base is given,

Why doesn't python take advantage of __iadd__ for sum and chained operators?

若如初见. 提交于 2019-12-08 15:58:15
问题 I just conducted an interesting test: ~$ python3 # I also conducted this on python 2.7.6, with the same result Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): ... def __add__(self, other): ... global add_calls ... add_calls += 1 ... return Foo() ... def __iadd__(self, other): ... return self ... >>> add_calls = 0 >>> a = list(map(lambda x:Foo(), range(6))) >>> a[0] + a[1] + a[2] <_

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?

末鹿安然 提交于 2019-12-08 04:26:00
问题 In Python 3, is a list comprehension simply syntactic sugar for a generator expression fed into the list function? e.g. is the following code: squares = [x**2 for x in range(1000)] actually converted in the background into the following? squares = list(x**2 for x in range(1000)) I know the output is identical, and Python 3 fixes the surprising side-effects to surrounding namespaces that list comprehensions had, but in terms of what the CPython interpreter does under the hood, is the former

What is the source code of __hash__() and __eq__() of object in Python?

人盡茶涼 提交于 2019-12-08 01:01:29
问题 object is a base for all new style classes. Where can I find the source code of object ? I'd like to have a look how the function __hash__() and __eq__() are defined. Refer to this answer (Finding the source code for built-in Python functions?), I search the object definition in cpython. There is not __hash__() and __eq__() definiton in https://hg.python.org/cpython/file/tip/Objects/object.c. 回答1: The default implementations for __hash__ and __eq__ are inherited from the base object type. You

What's the difference between a 'function', 'method' and 'bound method' in Python 3?

落爺英雄遲暮 提交于 2019-12-07 01:50:49
问题 I've observed at least 3 types related to functions in Python 3: >>> class A(): ... def f(): pass ... >>> A.f <function A.f at 0x7fcaef304268> >>> A().f <bound method A.f of <__main__.A object at 0x7fcaef2fae80 >>> set.union <method 'union' of 'set' objects> I'm wondering what's the difference between 'function', 'method' and 'bound method'? Is 'method' a type equivalent to 'unbound method' in Python 2? 回答1: Is 'method' a type equivalent to 'unbound method' in Python 2? Kind-a-sort-a. But not

Why does it take longer to import a function from a module than the entire module itself?

痞子三分冷 提交于 2019-12-06 18:20:01
问题 Consider: >>> timeit.timeit('from win32com.client import Dispatch', number=100000) 0.18883283882571789 >>> timeit.timeit('import win32com.client', number=100000) 0.1275979248277963 It takes significantly longer to import only the Dispatch function rather than the entire module, which seems counter intuitive. Could someone explain why the overhead for taking a single function is so bad? Thanks! 回答1: That's because: from win32com.client import Dispatch is equivalent to: import win32com.client

What is the source code of __hash__() and __eq__() of object in Python?

不想你离开。 提交于 2019-12-06 11:17:35
object is a base for all new style classes. Where can I find the source code of object ? I'd like to have a look how the function __hash__() and __eq__() are defined. Refer to this answer ( Finding the source code for built-in Python functions? ), I search the object definition in cpython . There is not __hash__() and __eq__() definiton in https://hg.python.org/cpython/file/tip/Objects/object.c . The default implementations for __hash__ and __eq__ are inherited from the base object type. You can find its type definition in typeobject.c : PyTypeObject PyBaseObject_Type = { PyVarObject_HEAD_INIT

When does CPython garbage collect?

瘦欲@ 提交于 2019-12-06 08:07:00
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 hear how this differs in PyPy or other python implementations. The GC runs periodically based on the

What is the stack in Python?

喜欢而已 提交于 2019-12-05 20:48:06
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? 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 stack of recursive calls of the interpreter loop. The Python stack is a stack of Python frame objects,