Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'exercise': 0.04; 'value,': 0.04; 'cpython': 0.05; 'interpreter': 0.05; 'subject:Python': 0.06; 'variables': 0.07; 'assigning': 0.09; 'immutable': 0.09; 'python': 0.11; 'assume': 0.14; 'mostly': 0.14; 'caches': 0.16; 'different,': 0.16; 'efficiency.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer,': 0.16; 'list.i': 0.16; 'literals,': 0.16; 'objects.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'value.': 0.19; 'memory': 0.22; 'example': 0.22; 'aug': 0.22; 'either.': 0.24; 'integer': 0.24; 'pre': 0.24; 'this:': 0.26; 'header:In- Reply-To:1': 0.27; "doesn't": 0.30; 'friends,': 0.30; 'message- id:@mail.gmail.com': 0.30; '>>>>': 0.31; 'ok.': 0.31; 'objects': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doubt': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'mailing': 0.39; 'skip:u 10': 0.60; 'number,': 0.60; 'new': 0.61; "you're": 0.61; 'you.': 0.62; "you'll": 0.62; 'different': 0.65; 'taking': 0.65; 'to,': 0.72; 'concept.': 0.84; 'intern': 0.84; 'location?': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=iXEaPT07Pt2oHU5jZAFv/ioOQMGJ1kgUFBUriVvRDkk=; b=Kqt0Y/pmOIrW/FFZjrm4fkkPkQx52vjj++9K/MW4oOPQ8GMR1hbspt5dC8dU5BJyHy CiM75HvjujmRYLxEPgIEG/fI3DqBCv7LionegGQtTZSb6c4QaJrtQEL6MiQvFGkEYMvc 8qpQRGM/OWGzPgzHlZS/bx5dkpZNakF4BRIWlSXYROhjp633lGuQMd8ew4ZQF8y/nSmi +LC0WadV9hEZnsjfQyZ3vxDSg2LDMtFDlVRkHv4/QQmItnxmkQCtiMp0ELhEDOA8SCJS mRmjTbkH9nnleEuxDm5HBOlio/i8SQsSJrH1nLQtLCoTZHr1aso9SE2SoYc2eRmd4NUO S6NQ== MIME-Version: 1.0 X-Received: by 10.58.249.236 with SMTP id yx12mr2982909vec.25.1376152944561; Sat, 10 Aug 2013 09:42:24 -0700 (PDT) In-Reply-To: References: Date: Sat, 10 Aug 2013 17:42:24 +0100 Subject: Re: Python Basic Doubt From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376152947 news.xs4all.nl 15926 [2001:888:2000:d::a6]:38511 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52311 On Sat, Aug 10, 2013 at 4:33 PM, Krishnan Shankar 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