python-internals

Possible to get "value of address" in python?

Possible to get "value of address" in python? Question: In the following, I can see that when adding an integer in python, it adds the integer, assigns that resultant value to a new memory address and then sets the variable to point to that memory address: >>> a=100 >>> id(a) 4304852448 >>> a+=5 >>> id(a) …

Total answers: 2

Different object size of True and False in Python 3

Different object size of True and False in Python 3 Question: 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 >>> True.__sizeof__() 28 What changed in Python 3 that makes the size of True …

Total answers: 4

Accessing dictionary items by position in Python 3.6+ efficiently

Accessing dictionary items by position in Python 3.6+ efficiently Question: I understand dictionaries are insertion ordered in Python 3.6+, as an implementation detail in 3.6 and official in 3.7+. Given they are ordered, it seems strange that no methods exist to retrieve the ith item of a dictionary by insertion order. The only solutions available …

Total answers: 2

Why do two identical lists have a different memory footprint?

Why do two identical lists have a different memory footprint? Question: I created two lists l1 and l2, but each one with a different creation method: import sys l1 = [None] * 10 l2 = [None for _ in range(10)] print(‘Size of l1 =’, sys.getsizeof(l1)) print(‘Size of l2 =’, sys.getsizeof(l2)) But the output surprised me: …

Total answers: 3

python generators garbage collection

python generators garbage collection Question: I think my question is related to this, but not exactly similar. Consider this code: def countdown(n): try: while n > 0: yield n n -= 1 finally: print(‘In the finally block’) def main(): for n in countdown(10): if n == 5: break print(‘Counting… ‘, n) print(‘Finished counting’) main() The …

Total answers: 2

Converting a series of ints to strings – Why is apply much faster than astype?

Converting a series of ints to strings – Why is apply much faster than astype? Question: I have a pandas.Series containing integers, but I need to convert these to strings for some downstream tools. So suppose I had a Series object: import numpy as np import pandas as pd x = pd.Series(np.random.randint(0, 100, 1000000)) On …

Total answers: 2

Is it possible to "hack" Python's print function?

Is it possible to "hack" Python's print function? Question: Note: This question is for informational purposes only. I am interested to see how deep into Python’s internals it is possible to go with this. Not very long ago, a discussion began inside a certain question regarding whether the strings passed to print statements could be …

Total answers: 4

Why is the size of 2⁶³ 36 bytes, but 2⁶³-1 is only 24 bytes?

Why is the size of 2⁶³ 36 bytes, but 2⁶³-1 is only 24 bytes? Question: Everything in Python is an object. So the size of an int in Python will be larger than usual. >>> sys.getsizeof(int()) 24 OK, but why does it take 12 more bytes for 2⁶³ compared too 2⁶³ – 1 and not …

Total answers: 2

Why are chained operator expressions slower than their expanded equivalent?

Why are chained operator expressions slower than their expanded equivalent? Question: 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 = …

Total answers: 2

Printing without parentheses varying error message using Python 3

Printing without parentheses varying error message using Python 3 Question: When I try to use print without parentheses on a simple name in Python 3.4 I get: >>> print max Traceback (most recent call last): … File “<interactive input>”, line 1 print max ^ SyntaxError: Missing parentheses in call to ‘print’ Ok, now I get …

Total answers: 4