Reputation: 17
I've been learning python and i've got a doubt and i'm not sure if what i'm thinking is the correct. As you know Python is an OOP Language and all objects have an id, type and a value. However there's a concept that i'm not sure if i understood well. It's the mutable and immutable objects. I know there are some objects that are mutables like arrays, lists and there are others that are immutable like strings, ints, tuples etc. Basically the main diference is that the immutable can't change its value. For example if i've got an int var:
x = 1
its value is always the same.
After that line of code if i create another var x but now = 2, it's another object, because they have different id's, right? But now, how can i access a var by id, for example my first x var?
Hope you can help. Thanks! :)
Upvotes: 0
Views: 115
Reputation: 101959
But now, how can i access a var by id, for example my first x var?
You can't. When you do:
x = 1
x = 2
when python executes the x = 2
the 1
object you assigned to x
doesn't have any references that can reach it and thus you cannot access it in any way. There's no such a thing as get_object_from_id
that allows you to reach any object given its id
.
When an object doesn't have any reference to it, it is effectively unreachable. When the garbage collector will be run the object will be destroyed (actually, in CPython most of the objects are destroyed immediately, but there isn't any guarantee about when an object will be destroyed).
As Martijn pointed out, the id
of an object is just an implementation detail and it may be reused by the interpreter when an object is destroyed, hence, even from a theoretical point of view, a function such as get_object_from_id
can't be implemented in any sensible manner.
Also, assignments have nothing to do with mutable vs immutable types. You can see the difference between mutable and immutable types when you perform operations on them. For example:
some_list.append(1)
Adds 1
to some_list
(modifying it), without creating a new list object, while:
a = 1
a += 2
Here a
is not modified by increasing its value to 3
, but a new integer object of value 3
is assigned to a
.
You can use the id
to check that the a
s are different objects:
>>> a = 1
>>> id(a)
25889896
>>> a += 2
>>> id(a)
25889848
Note that relying on id
is a really poor choice. For example:
>>> a = (1, 2)
>>> b = (1, 2)
>>> a is b # is compare the ids just like: id(a) == id(b)
False
>>> a[0] is b[0]
True
As you can see the interpreter decided to re-use the same object 1
for two different tuples. This is an optimization used for small integers, that works because integers are immutable.
Bottom line: id
can only tell you if two things aren't the same thing at a certain moment in time. Any other use is just wrong.
Upvotes: 1
Reputation: 23624
This seems like more of a question about scope,.. for which I would recommend reading this.
http://docs.python.org/release/1.5.1p1/tut/scopes.html
say we have the following
x = 1
def PrintStuff(x):
print (x)
PrintStuff(2)
>>> 2
In the function PrintStuff
, x
is a local variable.
Outside the function PrintStuff
lies a global variable x
To get the global x
, there are multiple options, but for right now lets just use the globals()
function, which returns a dictionary containing all global variables.
x = 1
def PrintStuff(x):
print( x)
print( globals()["x"])
PrintStuff(2)
>>> 2
>>> 1
Similarly we could use the locals()
function to get the local x
def PrintStuff(x):
print( locals()["x"])
PrintStuff(2)
>>> 2
Upvotes: 0