On Sat, Aug 10, 2013 at 4:33 PM, Krishnan Shankar <i.am.song...@gmail.com> wrote: > Hi Fellow Python Friends, > > I am new to Python and recently subscribed to the mailing list.I have a > doubt regarding the basics of Python. Please help me in understanding the > below concept. > > So doubt is on variables and their contained value.
Tangential to this: Python doesn't have "variables" that "contain" anything, but rather has names that are bound to (point to, if you like) objects. You're mostly right, this is just a terminology point. > Why does in the below example from Interpreter exploration value of c take > pre existing memory location. > >>>> a=10 >>>> id(a) > 21665504 >>>> b=a >>>> id(b) > 21665504 >>>> c=10 >>>> id(c) > 21665504 > > I am actually assigning new value to c. But from the value of id() all three > variables take same location. With variables a and b it is ok. But why c > taking the same location? CPython caches a number of integer objects for efficiency. Whenever you ask for the integer 10, you'll get the _same_ integer 10. But if you try the same exercise with a much higher number, or with a different value, you should get a unique id. With immutable literals, the interpreter's allowed to reuse them. You don't normally care about the id() of an integer, and nor should you. Same goes for strings; the interpreter's allowed to intern them if it chooses. Generally, don't assume that they're different, don't assume they're the same either. ChrisA -- http://mail.python.org/mailman/listinfo/python-list