4 ms·
> Then I guess using the id of the tuples to show that they are using the same object doesn't exactly prove the point Without the `del a`, it does, because the
by ccmonnett 8y ago
> Then I guess using the id of the tuples to show that they are using the same object doesn't exactly prove the point
Without the `del a`, it does, because they both have active references. If they were unique objects, we'd see a unique ID for `b` as long as a reference to `a` is active.
> Strings seem to behave like tuples. When I delete a string and create a new one it creates a new object with a new address.... unless the strings are of the same length.
String _objects_ (as opposed to variables referring to them) are immutable in Python. They tend to be allocated anew, but for optimization reasons you can end up with cases where the string objects have the same ID. Like here:
>>> a = 'asdf'
>>> id(a)
4389881424
>>> a = 'qwerty'
>>> id(a)
4395015672
>>> a = 'asdf'
>>> id(a)
4389881424
'asdf' has the same ID with no deling involved because its object wasn't GC'd yet.
Below are some relevant links if you want to knock yourself out (some do) but I write an awful lot of Python and even for me this is well into the realm of "what happens when..." after a few beers or job interview trivia.
https://rushter.com/blog/python-lists-and-tuples/ https://rushter.com/blog/python-lists-and-tuples/
http://guilload.com/python-string-interning/ http://guilload.com/python-string-interning/