FAQ Database Discussion Community


Does Python automatically replace * 2 with << 1?

python,python-3.x,cpython,python-internals
I saw suggestions (see e.g. Is multiplication and division using shift operators in C actually faster?) that you should not manually replace multiplication with the shift operator, because the compiler has to do it automatically and shift operators decrease readability. I have written a simple test to check this: import...

Lambda-function comparison in python

python,lambda,python-internals
In python you cannot directly compare functions created by lambda expressions: >>> (lambda x: x+2) == (lambda x: x+2) False I made a routine to hash the disassembly. import sys import dis import hashlib import contextlib def get_lambda_hash(l, hasher=lambda x: hashlib.sha256(x).hexdigest()): @contextlib.contextmanager def capture(): from cStringIO import StringIO oldout, olderr...

Why does `mylist[:] = reversed(mylist)` work?

python,list,reverse,assign,python-internals
The following reverses a list "in-place" and works in Python 2 and 3: >>> mylist = [1, 2, 3, 4, 5] >>> mylist[:] = reversed(mylist) >>> mylist [5, 4, 3, 2, 1] Why/how? Since reversed gives me an iterator and doesn't copy the list beforehand, and since [:]= replaces "in-place",...

Is this calculation executed in Python?

python,python-internals
Disclaimer: I'm new to programming, but new to Python. This may be a pretty basic question. I have the following block of code: for x in range(0, 100): y = 1 + 1; Is the calculation of 1 + 1 in the second line executed 100 times? I have...

Why does six.py use custom class for finding MAXSIZE?

python,python-3.x,python-2.x,python-internals,six
I was going through the code for six.py in the django utils, which, for non Jython implementations, tries, to find the MAXSIZE for the int. Now, the way this is done is interesting - instead of catching an exception on the statement itself, the statement is wrapped within a __len__...

What is the mechanism that allows Python monkey patching in this instance?

python,global-variables,monkeypatching,python-internals,side-effects
Can someone explain the logic behind how this works with the Python interpreter? Is this behavior only thread local? Why does the assignment in the first module import persist after the second module import? I just had a long debugging session that came down to this. external_library.py def the_best(): print...

Why is 'x' in ('x',) faster than 'x' == 'x'?

python,performance,python-3.x,python-internals
>>> timeit.timeit("'x' in ('x',)") 0.04869917374131205 >>> timeit.timeit("'x' == 'x'") 0.06144205736110564 Also works for multiple options, both seem to grow linearly: >>> timeit.timeit("'x' in ('x', 'y')") 0.04866674801541748 >>> timeit.timeit("'x' == 'x' or 'x' == 'y'") 0.06565782838087131 >>> timeit.timeit("'x' in ('y', 'x')") 0.08975995576448526 >>> timeit.timeit("'x' == 'y' or 'x' == 'y'") 0.12992391047427532...

Why is `1000000000000000 in range(1000000000000001)` so fast in Python 3?

python,performance,python-3.x,range,python-internals
It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator. This being the case, I would have expected the following line to take an inordinate amount of time, because in order to determine...

Does the len() built-in function iterates through the collection to calculate its length, or does it access a collection's attribute? [duplicate]

python,python-internals
This question already has an answer here: Cost of len() function 4 answers Python has many built-in functions, and len() is one of them. Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range)...

Why are some float < integer comparisons four times slower than others?

python,performance,floating-point,cpython,python-internals
When comparing floats to integers, some pairs of values take much longer to be evaluated than other values of a similar magnitude. For example: >>> import timeit >>> timeit.timeit("562949953420000.7 < 562949953421000") # run 1 million times 0.5387085462592742 But if the float or integer is made smaller or larger by a...

How do chained comparisons in Python actually work?

python,comparison-operators,python-internals
The Python Doc for Comparisons says: Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found...

For sets S and T, why does Python's S -= T take O(len(T)) and not O(len(S))?

python,big-o,python-internals
The Set entries in this Python time complexity table say, and the comments below confirm, that S.difference_update(T) takes time O(len(T)) while S - T takes O(len(S)). The reason given is that the algorithm for the first is "for every element in T remove it from S", while the algorithm for...

How does str(list) work?

python,string,list,eval,python-internals
Why does str(list) returns how we see list on the console? How does str(list) work? (any reference to the CPython code for str(list))? >>> x = ['abc', 'def', 'ghi'] >>> str(x) "['abc', 'def', 'ghi']" To get the original list back from the str(list) I have to: >>> from ast import...

Why doesn't Python optimize away temporary variables?

python,optimization,refactoring,python-internals
Fowler's Extract Variable refactoring method, formerly Introduce Explaining Variable, says use a temporary variable to make code clearer for humans. The idea is to elucidate complex code by introducing an otherwise unneeded local variable, and naming that variable for exposition purposes. It also advocates this kind of explaining over comments.....

Python is not searching the locals namespace

python,python-2.7,python-internals
I'm trying to import a function from another module and I can't use the import keyword because the module's name needs looking up in a list. If I try to call ExampleFunc normally i get NameError: global name 'ExampleFunc' is not defined but if I explicitly tell python to look...

Why doesn't the namedtuple module use a metaclass to create nt class objects?

python,metaclass,python-internals,namedtuple
I spent some time investigating the collections.namedtuple module a few weeks ago. The module uses a factory function which populates the dynamic data (the name of the new namedtuple class, and the class attribute names) into a very large string. Then exec is executed with the string (which represents the...