Variables with cross-module usage

Nitin Changlani. changlani.nitin at gmail.com
Sun Nov 29 17:06:57 EST 2009


Thanks Dennis and Steve,

This explains it all! I will discard using one.a and use one.myList[0]
directly, instead. I really appreciate your patience and the elaboration of
the concept.

Warm Regards,
Nitin Changlani.

On Sun, Nov 29, 2009 at 1:02 AM, Steven D'Aprano <
steve at remove-this-cybersource.com.au> wrote:

> On Sat, 28 Nov 2009 22:18:11 -0500, Nitin Changlani wrote:
>
> > Thanks for the reply MRAB, Rami, Matt and Mel,
> >
> > I was assuming that since one.myList0] = one.a, the change in one.a will
> > ultimately trickle down to myList[0] whenever myList[0] is printed or
> > used in an expression. It doesn't come intuitively to me as to why that
> > should not happen. Can you kindly suggest what is the correct way to go
> > about it?
>
>
> You are confusing *names* and *objects*. The presence or absence of a
> module qualifier is irrelevant, so for simplicity I will avoid it. I will
> use ` ` quotation marks to refer to names, to avoid confusing them with
> strings.
>
>
> The Python statement
>
> a = "some text"
>
> creates a name `a` which is bound to the object "some text".
>
> myList[0] = a
>
> stores the object bound to the name `a` to the first position of myList,
> not the name itself. So myList[0] ends up being "some text", but it has
> no idea whether it came from the name `a` or somewhere else.
>
> Then when you execute:
>
> a = "different text"
>
> the name `a` is bound to the object "different text". But this doesn't
> affect myList[0] at all, because you're not changing the object "some
> text" -- strings are immutable and can't be changed.
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>

> Thanks for the reply MRAB, Rami, Matt and Mel,
>
> I was assuming that since one.myList0] = one.a, the change in one.a will
> ultimately trickle down to myList[0] whenever myList[0] is printed or used
> in an expression. It doesn't come intuitively to me as to why that should
> not happen. Can you kindly suggest what is the correct way to go about it?
>

       First you understand that no common programming language behaves
this way... It's not just Python.

       It's just more subtle in Python.

       In classical languages "one.myList[0]" represents a location (in
this case, think of a room of file cabinets). "one" is a cabinet in the
room; myList is a drawer in the cabinet; [0] is a folder in the drawer.
and "a" is another drawer.

       In this classical language "one.myList[0] = one.a" means "open up
the drawer a in cabinet one, make a COPY of what it contains, and put
that copy into the [0] folder inside drawer myList in cabinet one.

       In these languages, the names always refer to the same location.
Python confuses matters by having names that don't really refer to
location, but are attached to the objects.

       "one" is a name attached to an object (the module). "a" is a name
"inside" the object "one" which is attached to some other object,
whatever it is. Similarly, "myList" is a name attached to an object (an
indexed list or a keyed dictionary). "[0]" is a "name" (the index or
key) into the object the name "myList" is attached to.

       "one.myList[0] = one.a" means "find the object with the name 'one.a'
attached to it, and attach then name 'one.myList[0]' to the same object"
Later, if you do "one.a = something", you say "find the object with name
'something' and attach the name 'one.a' to it" -- it is, in effect, the
name that is moved from object to object -- no copy is made.



--
       Wulfraed         Dennis Lee Bieber               KD6MOG
       wlfraed at ix.netcom.com
HTTP://wlfraed.home.netcom.com/<http://wlfraed.home.netcom.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091129/12b012f9/attachment-0001.html>


More information about the Python-list mailing list