2

After getting in touch with the more deeper workings of Python, I've come to the understanding that assigning a variable equals to the creation of a new object which has their own specific address regardless of the variable-name that was assigned to the object.

In that case though, it makes me wonder what happens to an object that was created and modified later on. Does it sit there and consumes memory?

The scenarion in mind looks something like this:

# Creates object with id 10101001 (random numbers)
x = 5

# Creates object with id 10010010 using the value from object 10101001.
x += 10

What happens to object with the id 10101001? Out of curiosity too, why do objects need an ID AND a refrence that is the variable name, wouldn't it be better to just assign the address with the variable name?

I apologize in advance for the gringe this question might invoke in someone.

4
  • 2
    'x=5 creates a new object' ...not quite true: stackoverflow.com/questions/306313/… Commented Oct 4, 2015 at 14:56
  • 1
    You may find this article helpful: Facts and myths about Python names and values, which was written by SO veteran Ned Batchelder. Commented Oct 4, 2015 at 15:01
  • @PM 2Ring Does this mean though, that if I have two variables with the same value but not refrecing each other, that there's actually just one object but multiple "Tags?", like: x= 5; y = 5 but there's only one integer object with the value 5? Commented Oct 6, 2015 at 8:11
  • 1
    For small integers, yes, x & y will both reference the same integer object. For larger integers, they can both reference the same object, but they might not. Try this in the interpreter: x=5;y=5;id(x),id(y);x+=1;id(x);x-=1;id(x). Now change it to x=500;y=500 Commented Oct 6, 2015 at 8:18

2 Answers 2

2

Here is a great talk that was given at PyCon by Ned Batchelder this year about how Python manages variables.

https://www.youtube.com/watch?v=_AEJHKGk9ns

I think it will help clear up some of your confusion.

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

1 Comment

That was confusing but yet so very clearing, thanks!
2

First of all Augmented assignment statements states:

An augmented assignment expression like x += 1 can be rewritten x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.

So depending on the type of x this might not create a new object.

Python is reference counted. So the reference count of the object with id 10101001 decremented. If this count hits zero, the is freed almost immediately. But most low range integers are cached anyways. Refer to Objects, Types and Reference Counts for all the details.

Regarding the id of an object:

CPython implementation detail: This is the address of the object in memory.

So basically id and reference are the same. The variable name is just a binding to the object itself.

Comments

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.