Questions tagged [python-internals]

How does Python work underneath the hood? Use for questions relating to (for instance) the design decisions made and the internal data structures and algorithms used.

This tag is to be used for those posts relating to the internal working of code written in the programming language.

The scope of this tag includes the standard library [Python 2, Python 3].

732 questions
112
votes
2 answers

Does Python optimize away a variable that's only used as a return value?

Is there any ultimate difference between the following two code snippets? The first assigns a value to a variable in a function and then returns that variable. The second function just returns the value directly. Does Python turn them into…
Jayesh
  • 4,755
  • 9
  • 32
  • 62
103
votes
4 answers

When is hash(n) == n in Python?

I've been playing with Python's hash function. For small integers, it appears hash(n) == n always. However this does not extend to large numbers: >>> hash(2**100) == 2**100 False I'm not surprised, I understand hash takes a finite range of values.…
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
102
votes
3 answers

What exactly is __weakref__ in Python?

Surprisingly, there's no explicit documentation for __weakref__. Weak references are explained here. __weakref__ is also shortly mentioned in the documentation of __slots__. But I could not find anything about __weakref__ itself. What exactly is…
Michael
  • 7,407
  • 8
  • 41
  • 84
101
votes
2 answers

When are .pyc files refreshed?

I understand that ".pyc" files are compiled versions of the plain-text ".py" files, created at runtime to make programs run faster. However I have observed a few things: Upon modification of "py" files, program behavior changes. This indicates that…
Aaron Schif
  • 2,421
  • 3
  • 17
  • 29
100
votes
3 answers

Can we make 1 == 2 true?

Python ints are objects that encapsulate the actual number value. Can we mess with that value, for example setting the value of the object 1 to 2? So that 1 == 2 becomes True?
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
98
votes
1 answer

Why does tuple(set([1,"a","b","c","z","f"])) == tuple(set(["a","b","c","z","f",1])) 85% of the time with hash randomization enabled?

Given Zero Piraeus' answer to another question, we have that x = tuple(set([1, "a", "b", "c", "z", "f"])) y = tuple(set(["a", "b", "c", "z", "f", 1])) print(x == y) Prints True about 85% of the time with hash randomization enabled. Why 85%?
Veedrac
  • 58,273
  • 15
  • 112
  • 169
93
votes
3 answers

Why is max slower than sort?

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…
WeizhongTu
  • 6,124
  • 4
  • 37
  • 51
91
votes
4 answers

Why is copying a shuffled list much slower?

Copying a shuffled range(10**6) list ten times takes me about 0.18 seconds: (these are five runs) 0.175597017661 0.173731403198 0.178601711594 0.180330912952 0.180811964451 Copying the unshuffled list ten times takes me about 0.05…
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
82
votes
2 answers

list() uses slightly more memory than list comprehension

So i was playing with list objects and found little strange thing that if list is created with list() it uses more memory, than list comprehension? I'm using Python 3.5.2 In [1]: import sys In [2]: a = list(range(100)) In [3]:…
vishes_shell
  • 22,409
  • 6
  • 71
  • 81
80
votes
1 answer

Python: why are * and ** faster than / and sqrt()?

While optimising my code I realised the following: >>> from timeit import Timer as T >>> T(lambda : 1234567890 / 4.0).repeat() [0.22256922721862793, 0.20560789108276367, 0.20530295372009277] >>> from __future__ import division >>> T(lambda :…
mac
  • 42,153
  • 26
  • 121
  • 131
79
votes
2 answers

Why is code using intermediate variables faster than code without?

I have encountered this weird behavior and failed to explain it. These are the benchmarks: py -3 -m timeit "tuple(range(2000)) == tuple(range(2000))" 10000 loops, best of 3: 97.7 usec per loop py -3 -m timeit "a = tuple(range(2000)); b =…
Bharel
  • 23,672
  • 5
  • 40
  • 80
79
votes
6 answers

Why can I use the same name for iterator and sequence in a Python for loop?

This is more of a conceptual question. I recently saw a piece of code in Python (it worked in 2.7, and it might also have been run in 2.5 as well) in which a for loop used the same name for both the list that was being iterated over and the item in…
Gustav
  • 688
  • 5
  • 12
77
votes
2 answers

Why is a trailing comma a SyntaxError in an argument list that uses *args syntax?

Why can't you use a trailing comma with *args in Python? In other words, this works >>> f(1, 2, b=4,) But this does not >>> f(*(1, 2), b=4,) File "", line 1 f(*(1, 2), b=4,) ^ SyntaxError: invalid syntax This is…
asmeurer
  • 86,894
  • 26
  • 169
  • 240
76
votes
4 answers

Different object size of True and False in Python 3

Experimenting with magic methods (__sizeof__ in particular) on different Python objects I stumbled over the following behaviour: Python 2.7 >>> False.__sizeof__() 24 >>> True.__sizeof__() 24 Python 3.x >>> False.__sizeof__() 24 >>>…
Simon Fromme
  • 3,104
  • 18
  • 30
74
votes
5 answers

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

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 is True. Why is…
root
  • 1,066
  • 1
  • 8
  • 16
1 2
3
48 49