python-internals

Why is max slower than sort?

Why is max slower than sort? Question: I’ve found that max is slower than the sort function in Python 2 and 3. Python 2 $ python -m timeit -s ‘import random;a=range(10000);random.shuffle(a)’ ‘a.sort();a[-1]’ 1000 loops, best of 3: 239 usec per loop $ python -m timeit -s ‘import random;a=range(10000);random.shuffle(a)’ ‘max(a)’ 1000 loops, best of 3: 342 …

Total answers: 3

Why is range(0) == range(2, 2, 2) True in Python 3?

Why is range(0) == range(2, 2, 2) True in Python 3? Question: Why do ranges which are initialized with different values compare equal to one another in Python 3? When I execute the following commands in my interpreter: >>> r1 = range(0) >>> r2 = range(2, 2, 2) >>> r1 == r2 True The result …

Total answers: 5

Why is str.translate much faster in Python 3.5 compared to Python 3.4?

Why is str.translate much faster in Python 3.5 compared to Python 3.4? Question: I was trying to remove unwanted characters from a given string using text.translate() in Python 3.4. The minimal code is: import sys s = ‘abcde12345@#@$#%$’ mapper = dict.fromkeys(i for i in range(sys.maxunicode) if chr(i) in ‘@#$’) print(s.translate(mapper)) It works as expected. However …

Total answers: 1

The `is` operator behaves unexpectedly with non-cached integers

The `is` operator behaves unexpectedly with non-cached integers Question: When playing around with the Python interpreter, I stumbled upon this conflicting case regarding the is operator: If the evaluation takes place in the function it returns True, if it is done outside it returns False. >>> def func(): … a = 1000 … b = …

Total answers: 2

Identifier normalization: Why is the micro sign converted into the Greek letter mu?

Identifier normalization: Why is the micro sign converted into the Greek letter mu? Question: I just stumbled upon the following odd situation: >>> class Test: µ = ‘foo’ >>> Test.µ ‘foo’ >>> getattr(Test, ‘µ’) Traceback (most recent call last): File “<pyshell#4>”, line 1, in <module> getattr(Test, ‘µ’) AttributeError: type object ‘Test’ has no attribute ‘µ’ …

Total answers: 2

Why is a class __dict__ a mappingproxy?

Why is a class __dict__ a mappingproxy? Question: I wonder why a class __dict__ is a mappingproxy, but an instance __dict__ is just a plain dict >>> class A: … pass >>> a = A() >>> type(a.__dict__) <class ‘dict’> >>> type(A.__dict__) <class ‘mappingproxy’> Asked By: José Luis || Source Answers: This helps the interpreter assure …

Total answers: 3

Complexity of len() with regard to sets and lists

Complexity of len() with regard to sets and lists Question: The complexity of len() with regards to sets and lists is equally O(1). How come it takes more time to process sets? ~$ python -m timeit “a=[1,2,3,4,5,6,7,8,9,10];len(a)” 10000000 loops, best of 3: 0.168 usec per loop ~$ python -m timeit “a={1,2,3,4,5,6,7,8,9,10};len(a)” 1000000 loops, best of …

Total answers: 7

Why is string's startswith slower than in?

Why is string's startswith slower than in? Question: Surprisingly, I find startswith is slower than in: In [10]: s=”ABCD”*10 In [11]: %timeit s.startswith(“XYZ”) 1000000 loops, best of 3: 307 ns per loop In [12]: %timeit “XYZ” in s 10000000 loops, best of 3: 81.7 ns per loop As we all know, the in operation needs …

Total answers: 2

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

Why are some float < integer comparisons four times slower than others? Question: 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 …

Total answers: 2

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

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3? Question: 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 …

Total answers: 4