0

I was coding a test program that test many functions in Python to learn more deeper about its programming language. I knew that when we assign a variable with another variable, the assigned variable will get the same id from the another variable... But after that, I'm wondering that can we assign a variable with another variable value WITHOUT getting it the same id from that another variable? Hope that in Python has that way to do it because I really need it for my little Pygame project, thanks.

3
  • 4
    docs.python.org/3/library/copy.html Commented Aug 5, 2022 at 12:14
  • 2
    Variables don't have IDs; the objects in them do. So if the objects in 2 different variables have the same ID, it means they are the same object. Which is how you get to @Wimanicesir's comment. Commented Aug 5, 2022 at 12:21
  • It is uncommon to need to use the id of an object in normal application code. What is it about your Pygame project that requires this? Commented Aug 5, 2022 at 14:30

1 Answer 1

1

You are right that assignment operators just bind the same object to the new variable. In general you can alternatively construct a new object by using copy or some other method. But this won't always work for every kind of value.

In some cases it is not even possible for the same value to have a different id. For example try:

x = 1
y = 1
x is y

And it will likely return True, meaning x and y reference the same object with the same id. That is the implementation of python uses only one instance for integer objects with a value of 1.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, so now I can copy the value of the varible without getting assigned and having same id by using copy methods like copy.copy() from moudle copy to copy it's only value without reference into them both object! Thank you so much Andrew!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.