python-internals

Fraction object doesn't have __int__ but int(Fraction(…)) still works

 ̄綄美尐妖づ 提交于 2019-12-01 19:43:57
In Python, when you have an object you can convert it to an integer using the int function. For example int(1.3) will return 1 . This works internally by using the __int__ magic method of the object, in this particular case float.__int__ . In Python Fraction objects can be used to construct exact fractions. from fractions import Fraction x = Fraction(4, 3) Fraction objects lack an __int__ method, but you can still call int() on them and get a sensible integer back. I was wondering how this was possible with no __int__ method being defined. In [38]: x = Fraction(4, 3) In [39]: int(x) Out[39]: 1

In Python 3.x, why is there not an itertools shared-object on disk?

a 夏天 提交于 2019-12-01 19:38:04
Is the itertools C module included somehow in the main Python binary in 3.x? Assuming that the C module is built and included, which it appears to be: >>> import inspect >>> import itertools >>> >>> inspect.getsourcefile(itertools) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 571, in getsourcefile filename = getfile(object) File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 518, in getfile raise

Python dictionary iterator performance

二次信任 提交于 2019-12-01 17:17:48
When working with dictionaries in Python, this page says that the time complexity of iterating through the element of the dictionary is O(n) , where n is the largest size the dictionary has been. However, I don't think that there is an obvious way to iterate through the elements of a hash table. Can I assume good performance of dict.iteritems() when iterating through element of a hash table, without too much overhead? Since dictionaries are used a lot in Python, I assume that this is implemented in some smart way. Still, I need to make sure. If you look at the notes on Python's dictionary

Python swapping lists

大城市里の小女人 提交于 2019-12-01 17:13:39
In python, when I assign a list to another, like: a = [1,2,3] b = a Now b and a point to the same list. Now considering two lists, a = [1,2,3] b = [4,5,6] a,b = b,a Now how is it that they are swapped like any other data type and does not end up both pointing to the same list? Looks like Python internally swaps the items. Check this program a, b = [1, 2], [2, 3] def func(): a, b = b, a import dis dis.dis(func) Output 4 0 LOAD_FAST 0 (b) 3 LOAD_FAST 1 (a) 6 ROT_TWO 7 STORE_FAST 1 (a) 10 STORE_FAST 0 (b) 13 LOAD_CONST 0 (None) 16 RETURN_VALUE So, Python pushes references from b and a in the

Python swapping lists

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 15:16:27
问题 In python, when I assign a list to another, like: a = [1,2,3] b = a Now b and a point to the same list. Now considering two lists, a = [1,2,3] b = [4,5,6] a,b = b,a Now how is it that they are swapped like any other data type and does not end up both pointing to the same list? 回答1: Looks like Python internally swaps the items. Check this program a, b = [1, 2], [2, 3] def func(): a, b = b, a import dis dis.dis(func) Output 4 0 LOAD_FAST 0 (b) 3 LOAD_FAST 1 (a) 6 ROT_TWO 7 STORE_FAST 1 (a) 10

CPython: Why does += for strings change the id of string variable

江枫思渺然 提交于 2019-12-01 09:05:23
Cpython optimizes string increment operations,When initializing memory for a string, the program leaves extra expansion space for it,so, when incrementing, the original string is not copied to the new location. my question is why the id of string variable changes. >>> s = 'ab' >>> id(s) 991736112104 >>> s += 'cd' >>> id(s) 991736774080 why the id of string variable changes. The optimization you are trying to trigger is an implementation detail of CPython and is a quite subtle thing: there are many details (e.f. one you are experiencing) which can be preventing it. For a detailed explanation,

CPython: Why does += for strings change the id of string variable

寵の児 提交于 2019-12-01 07:13:39
问题 Cpython optimizes string increment operations,When initializing memory for a string, the program leaves extra expansion space for it,so, when incrementing, the original string is not copied to the new location. my question is why the id of string variable changes. >>> s = 'ab' >>> id(s) 991736112104 >>> s += 'cd' >>> id(s) 991736774080 why the id of string variable changes. 回答1: The optimization you are trying to trigger is an implementation detail of CPython and is a quite subtle thing:

size of generator object in python

扶醉桌前 提交于 2019-12-01 06:34:47
问题 For the following code: import sys x=(i for i in range(1,11)) print x print 'Before starting iterating generator size is' ,sys.getsizeof(x) print 'For first time' for i in x: print i print 'For second time , does not print anything' for i in x: print i # does not print anything print 'After iterating generator size is' ,sys.getsizeof(x) the output is: <generator object <genexpr> at 0x014C1A80> Before starting iterating generator size is 40 For first time 1 2 3 4 5 6 7 8 9 10 For second time

Writing (and not) to global variable in Python

て烟熏妆下的殇ゞ 提交于 2019-12-01 06:05:25
Coming from much less dynamic C++, I have some trouble understanding the behaviour of this Python (2.7) code. Note: I am aware that this is bad programming style / evil, but I would like to understand it non the less. vals = [1,2,3] def f(): vals[0] = 5 print 'inside', vals print 'outside', vals f() print 'outside', vals This code runs without error, and f manipulates the (seemingly) global list. This is contrary to my prior understanding that global variables that are to be manipulated (and not only read) in a function must be declared as global ... . On the other hand, if I replace vals[0] =

Integer File Descriptor “0” in open() - Python 3

冷暖自知 提交于 2019-12-01 04:17:27
In Python 3, it is possible to open a file object using an "integer file descriptor" with the format: stdout = open(1, "w") stdout.write("Hello World") # Prints Hello World stdout.close() Though, interestingly, I found that 0 is also a valid stream. If I put this in the file testio.py : stdout = open(0, "w") stdout.write("Foo Bar\n") stdout.close() And then run that code the output is: bash-3.2$ python3 testio.py Foo Bar Which seems just like stdout . However... bash-3.2$ python3 testio.py > testio.txt Foo Bar bash-3.2$ cat testio.txt So it seems that this is actually not stdout , but