Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'syntax': 0.03; 'subject:Python': 0.05; '#include': 0.07; 'assign': 0.07; 'main()': 0.07; 'python': 0.09; '"a"': 0.09; '40,': 0.09; 'malloc': 0.09; 'aug': 0.13; 'value.': 0.15; '"new': 0.16; '%d,': 0.16; '24,': 0.16; 'ah,': 0.16; 'allocates': 0.16; 'at,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'is).': 0.16; 'roy': 0.16; 'subject:Objects': 0.16; 'utterly': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'variables': 0.17; 'memory': 0.18; 'variable': 0.20; 'received:209.85.214.174': 0.21; "i've": 0.23; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'c++': 0.27; 'instead.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'all.': 0.28; 'run': 0.28; '>>>>': 0.29; 'closer': 0.29; 'thinks': 0.29; 'objects': 0.29; "i'm": 0.29; 'fri,': 0.30; 'code': 0.31; 'point': 0.31; 'could': 0.32; 'int': 0.33; 'values.': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'similar': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; "didn't": 0.36; 'possible': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'object': 0.38; 'nothing': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'notice': 0.39; 'header:Received:5': 0.40; 'address': 0.60; 'ever': 0.63; 'fact,': 0.69; 'smith': 0.71; 'emphasizing': 0.84 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=MmE+MyZBEXjQmIUejZ/gQBs/KnTcwmDvxdgOdhaVAPo=; b=cjSePfeBIrvea5Sl81JkxpNm1i+NxkzKc0Cu5SvVDq5YfpM1pqY8ohbB21SJ30lWjb icTwR6k0V0AR/R6Tp2I6lvk5GNVNZRS7MNmf0zWC58AOfT4U3M/HacZAzVSFalbU6cgF qBHCt3DZfGR6CjZm+R0GT3cLeqreZVPuuIibQNwm5ypgwqb0h2SmZ3Jq0gs9SJHhf2zu wyDj1c2OQbpK+r75DfIxQ8y2Jbj4rh/79pSarYXHPSn/iBttT2nEgGmOxCvBJYYgPaYS VPbGO83GW5oBvwn5I09CYl1Q3IyBWLCtX6xS4RanYtL5drN30e6PKwazrFUuBg05GHSZ yZiA== MIME-Version: 1.0 In-Reply-To: References: <18409992-1e28-4721-8e64-60c69668da4e@googlegroups.com> <87d32i1ntc.fsf@benfinney.id.au> Date: Fri, 24 Aug 2012 11:34:02 +1000 Subject: Re: Objects in Python 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.12 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345772045 news.xs4all.nl 6876 [2001:888:2000:d::a6]:59121 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27775 On Fri, Aug 24, 2012 at 10:36 AM, Roy Smith wrote: > In fact, I can even write it that way and everything works: > >>>> globals()["a"] = 42 >>>> a > 42 > > Even id() thinks they're the same thing: > >>>> id(a) > 1755402140 >>>> id(globals()["a"]) > 1755402140 Ah, no. What you have there is actually id(4) and nothing to do with a at all. > But, notice what happens if I now assign something new to a: > >>>> a = 123 >>>> id(a) > 1755403176 > > The id has changed! Now, we all know that the id of an object is its > memory address (that's not guaranteed, but in the standard C > implementation of Python, that's what it is). And you now have id(123) - of course, it's possible for there to be two integer objects with the value 123, but what I'm emphasizing is that you're not looking at a here. > Now, what if I do something similar in C: > > #include > > main() { > int a = 40; > printf("a = %d, &a = %p\n", a, &a); > a = 99; > printf("a = %d, &a = %p\n", a, &a); > } > > When I compile and run this, it prints: > > a = 40, &a = 0x7fff1911f5bc > a = 99, &a = 0x7fff1911f5bc > > Notice that the address of the variable "a" didn't change when I > assigned it a new value. That's what people mean when they say C has > variables and Python doesn't; it just binds names to values. Try this instead. It's C++ not C but a much closer match. You could instead play with malloc if you want it to be C. #include main() { int *a=new int(40); printf("a = %d, id(a) = %p\n",*a,a); a=new int(99); printf("a = %d, id(a) = %p\n",*a,a); } I've not tested the code and may have a syntax issue with "new int(40)" (who ever allocates a single int on the heap??) but you get the idea. At no point do you ever look at, or need to look at, &a. That's utterly irrelevant. ChrisA