Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52311

Re: Python Basic Doubt

References <CAL0E0u6wO_UBniWoSpePvhKhPDG_nf4p1rqYYrGwzoHTqp6ZHA@mail.gmail.com>
Date 2013-08-10 17:42 +0100
Subject Re: Python Basic Doubt
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.430.1376152947.1251.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Aug 10, 2013 at 4:33 PM, Krishnan Shankar
<i.am.songoku@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python Basic Doubt Chris Angelico <rosuav@gmail.com> - 2013-08-10 17:42 +0100

csiph-web