python-internals

Updating a set while iterating over its elements

回眸只為那壹抹淺笑 提交于 2019-12-20 15:29:31
问题 When I try to update a set while iterating over its elements, what should be its behavior? I tried it over various scenarios and it does not iterate over elements added after iteration is started and also the elements removed during iteration. If I remove and put back any element during iteration, that element is being considered. What's the exact behavior and how does it work? This prints all the permutations of a string: def permutations(s): ans = [] def helper(created, remaining): if len

Python's Passing by References [duplicate]

China☆狼群 提交于 2019-12-20 09:48:52
问题 This question already has answers here : “is” operator behaves unexpectedly with integers (11 answers) Closed 2 years ago . Hello I am trying to understand how Python's pass by reference works. I have an example: >>>a = 1 >>>b = 1 >>>id(a);id(b) 140522779858088 140522779858088 This makes perfect sense since a and b are both referencing the same value that they would have the identity. What I dont quite understand is how this example: >>>a = 4.4 >>>b = 1.0+3.4 >>>id(a);id(b) 140522778796184

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

☆樱花仙子☆ 提交于 2019-12-19 21:53:10
问题 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

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

天大地大妈咪最大 提交于 2019-12-19 21:23:11
问题 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

Writing (and not) to global variable in Python

≡放荡痞女 提交于 2019-12-19 07:28:39
问题 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

Writing (and not) to global variable in Python

半腔热情 提交于 2019-12-19 07:28:18
问题 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

Finding the source code of methods implemented in C?

旧时模样 提交于 2019-12-18 18:08:11
问题 Please note I am asking this question for informational purposes only I know the title sound like a duplicate of Finding the source code for built-in Python functions?. But let me explain. Say for example, I want to find the source code of most_common method of collections.Counter class. Since the Counter class is implemented in python I could use the inspect module get it's source code. ie, >>> import inspect >>> import collections >>> print(inspect.getsource(collections.Counter.most_common)

Why are chained operator expressions slower than their expanded equivalent?

℡╲_俬逩灬. 提交于 2019-12-18 14:48:17
问题 In python, it is possible to chain operators in this manner: a op b op c Which is evaluated to a op b and b op c With the only difference being that b is evaluated only once (so, something more like t = eval(b); a op t and t op c ). This is advantageous from the view point that it is very readable and more concise than the equivalent version with explicit conjunction (using and ). However... I've noticed that there is a minor performance difference between chained expressions and the

Order of insertion in sets (when parsing {}) [duplicate]

丶灬走出姿态 提交于 2019-12-18 13:09:20
问题 This question already has an answer here : Dict/Set Parsing Order Consistency (1 answer) Closed 2 years ago . Someone asked here why when putting 1 and True in a set only 1 is kept. This is of course because 1==True . But in which cases 1 is kept and in which cases True is kept? Let's see: passing a list to build the set instead of using the set notation: >>> set([True,1]) {True} >>> set([1,True]) {1} seems logical: set iterates on the inner list, and doesn't add the second element because it

Why does the OrderedDict keys view compare order-insensitive?

你。 提交于 2019-12-18 13:04:23
问题 Why does the OrderedDict keys view compare order-insensitive? >>> from collections import OrderedDict >>> xy = OrderedDict([('x', None), ('y', None)]) >>> yx = OrderedDict([('y', None), ('x', None)]) >>> xy == yx False >>> xy.keys() == yx.keys() True The OrderedDict keys view should arguably behave like an OrderedSet, but instead it behaves the same as dict.keys (i.e. like a usual set ). Same "issue" in python2: >>> xy.viewkeys() == yx.viewkeys() True They are different types, ( odict_keys is