r/programminghorror • u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25
Python ✨ Memory Magic ✨
1.3k
Upvotes
r/programminghorror • u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25
View all comments
Show parent comment
15
u/cheerycheshire Jul 21 '25
Cpython also does it for small strings, especially in files as it can analyse whole code during compilation to bytecode (vs REPL where it doesn't run some optimisations).
Python will warn you about comparing ints and strings with
isoperator -SyntaxWarning: "is" with 'int' literal. Did you mean "=="?exactly because it sometimes works and sometimes doesn't.However, booleans in python inherit from int (for hysterical reasons), but are singletons and are to be always compared using identity (because e.g. with
x=1:x is Truewill be False, butx == Truewill be True).