FAQ Database Discussion Community
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...
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)...
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...
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...
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...
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",...
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...
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,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...
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...
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...
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...
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...
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...
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__...
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...