Variables with cross-module usage

2009-11-28 Thread Nitin Changlani.
Hello everyone,

I am fairly new to Python and occasionally run into problems that are almost
always resolved by referring to this mailing-list's archives. However, I
have this one issue which has got me stuck and I hope you will be tolerant
enough to help em out with it!

What I want to achieve is something like the global variables in C/C++: you
declare them in one file and "extern" them in all the files where you need
to use them. I have 3 files: one.py, two.py and three.py as below:

one.py
--
a = 'place_a'
b = 'place_b'
x = 'place_x'

myList = [a, b, 'place_c']

==

two.py
--
import one

def myFunc():
print one.x
print one.myList

==

three.py

import one
import two

def argFunc():
one.x = 'place_no_x'
one.a = 'place_no_a'
one.b = 'place_no_b'

if __name__ == '__main__':
two.myFunc()
print
argFunc()
two.myFunc()

==

Output:
---
'place_x'
['place_a', 'place_b', 'place_c']

'place_no_x'
['place_a', 'place_b', 'place_c'] (*** instead of ['place_no_a',
'place_no_b', 'place_c'] ***)

The last line in the output is what's baffling me. Can anyone please help me
know if I am doing something wrong?

Thanks in advance,
Nitin.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables with cross-module usage

2009-11-28 Thread Nitin Changlani.
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?

Nitin.

>
>
>
> > Hi Nitin,
> >
> > On Sat, Nov 28, 2009 at 14:36, MRAB  wrote:
> >> Nitin Changlani. wrote:
> >>> three.py
> >>> 
> >>> import one
> >>> import two
> >>>
> >>> def argFunc():
> >>> one.x = 'place_no_x'
> >>> one.a = 'place_no_a'
> >>> one.b = 'place_no_b'
> >>>
> >
> > I think this is what is biting you. You might expect that after
> > argFunc, one.x would be set to 'place_no_x' and so on. However,
> > Python's scoping doesn't work like that -- the name one.x is only
> > rebound in the function's scope, so outside of argFunc (e.g. in your
> > main printing code) one.x is still bound to 'place_x'.
>
> No, It's not like that.  MRAB had it.  The thing is, that when one.py is
> imported, it sets the name one.a to refer to a string 'place_a'.  Then a
> list named one.myList is built with one.myList[0] referring to the same
> string as one.a .  So far, so good.
>
> Then the function argFunc is called.  It uses `one` as a name from its
> global namespace.  Inside argFunc, the names one.x and one.a are rebound to
> different strings from the ones they started with.  *But* one.myList[0]
> isn't touched, and still refers to 'place_x' like it always did.
>
>Mel.
>
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables with cross-module usage

2009-11-28 Thread Nitin Changlani
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?

Nitin.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables with cross-module usage

2009-11-29 Thread Nitin Changlani.
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 <
st...@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
   wlfr...@ix.netcom.com
HTTP://wlfraed.home.netcom.com/<http://wlfraed.home.netcom.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list