0

My question is with reference to this discussion here: Unnamed Python objects have the same id

Why is that these 2 range objects assigned to variables have "overlapping lifetimes"?

 x = range(3)
 y = range(3)

In what sense are they overlapping? If I delete variable x using the command: del x then the range object associated with x is GC'd since there is no longer a reference to it. But range object associated with variable y is still present. So how are lifetimes overlapping?

Also, I am not very clear about when lifetimes overlap and when they do not. Can this be explained in some simple terms? Assume Python version 3.6.5.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
pchegoor
  • 386
  • 1
  • 5
  • 14
  • 3
    Why did you think they *wouldn't* have overlapping lifetimes? You don't `del x` *before* creating the second `range` object and assigning it to `y`, so they necessarily both have non-zero reference counts at the same time. – jonrsharpe Jun 14 '18 at 16:04
  • 1
    "Overlapping" does not mean "identical", and it does not mean the objects need to die together. – user2357112 Jun 14 '18 at 16:14
  • @user2357112 So when do objects not overlap? And when they do not overlap the id function value for these objects may or may not be the same value. Is this correct? Thanks. – pchegoor Jun 14 '18 at 17:45
  • `x = range(3); del x; y = range(3)` would be an example where they don't overlap. – Mark Plotnick Jun 14 '18 at 19:55
  • @MarkPlotnick Thanks. I also noticed a scenario where they do not overlap. `id(range(3)) == id(range(3))` . Unable to understand why they do not overlap here. Also, this does not seem to be True in all cases for eg `id(3) == id(5)`. – pchegoor Jun 14 '18 at 20:39

0 Answers0