

3·
4 months agolot of copied variables, because of c I suppose? Things like var = self.var
If we’re very charitable, there’s a micro-optimization w/ Python (or at least, older Python) where assigning to a local variable like this inside a method is faster than the full self.var lookup, so you’ll see it in Python’s library code while setting up some loops, etc. as a small speedup. “lots of copied variables”, though, is likely an anti-pattern if not in a heavily used piece of library code, imo.
What’s really crazy is when people write modified Python language pre-processors where:
var = var
is a necessary thing (to bring the var into the right context for the pre-processor to recognize it; yes, I’ve seen this…)
IMO, using id() as a key would never be a good idea under any circumstance.
Two different (and even unequal) objects can have the same id():
>>> x = [1] >>> id(x) 4527263424 >>> del x >>> x = [2] >>> id(x) 4527263424 >>> del x >>> y = [3] >>> id(y) 4527263424Note - a dictionary lookup already looks up the key by id() first as a shortcut (under-the-hood), so there’s no need to try doing this as an optimization.
Edit: in case it wasn’t clear above, the object with the same id()s don’t all exist at the same time; but if you store their ids as a key, you’d have to ensure the object lifetimes are identical to be sure the ids could identify the same stored value. The dictionary does this for you when you use the key object, but it’s not automatic when using the id of the key.
Other Note - Since you phrased it as “all ints below a fixed limit share the same id() result”, I’d suggest a better way to semantically think of it is that certain constant objects are pre-allocated, and thus are kinda like singletons. There is usually only one
int(1)object, and the language keeps a pre-allocated pool of these common small ints (since they are used so often for indexing anyway).Similarly, many short string constants are ‘interned’ in a similar way; they aren’t pre-created at startup, but once they are created by the user declaring a string constant when the code is run, it saves memory to check and only keep one copy of those string objects, as the string constants can be checked at byte-compile time. But if you construct the string with code, it doesn’t bother to check all the strings to see if there exists an identical one. So for example:
>>> x = 'ab' >>> y = 'ab' >>> id(x) == id(y) True >>> s = 'a' >>> s = s + 'b' >>> id(s) == id(x) False >>> s == x == y TrueBut you can force it to make this check; it’s something they made more tedious to do in Python3 since it’s really an implementation detail:
>>> import sys >>> s = sys.intern(s) >>> id(s) == id(x) TrueSorry for the verbose reply; hope it helped.