Can I make different variables of the same value point to different objects?… here is a solution to the problem.
Can I make different variables of the same value point to different objects?
I
think this question may already have an answer on SO, but I can’t seem to find one. If you find the answer, mark it as a duplicate.
I’m not asking why, but how can I not let this happen?
I want j
to have a different id
than i
.
Say when to do it,
>>> i = 6
>>> j = 6
>>> id(i)
10919584
>>> id(j)
10919584 #I don't want this, I want j to point to a different object
So, I understand what’s happening in the code above (or at least I think I do), but my question is how do I prevent it from happening?
I am asking it just out of curiosity, it may or may not have any practical usage or relevance.
Solution
The possible use for this is a bit strange; The option I found :
class NoFixedInt(int):
pass
a = NoFixedInt(6)
b = NoFixedInt(6)
c = NoFixedInt(6)
print id(a)
# 4485155368
print id(b)
# 4485155656
print id(c)
# 4485155728
Of course, I don’t know if this is right for you because it has the problem that you have to convert everything, but it works.