How to do * and & C-lang. operations in Python?

Donn Cave donn at u.washington.edu
Thu Sep 20 13:29:53 EDT 2001


Quoth gaya at infomail.lacaixa.es (Toni Gaya):
| I have a variable defined on one module, and want to use _exactly_ the
| same variable in other module. Example:
|
| module1:
| 	a = 1
|
| module 2:
| 	import module1
| 	a = module1.a
| 	Now, I have module1.a value in a, not a reference to module1.a in a
| (module2.a).

No, you actually do have a reference to module1.a.  But as you
must have noticed, when you later assign another value to "a",
module1.a is not affected, and the converse as well.  This may
seem like semantic nitpicking, but you'll see that it's seriously
incorrect to think of this as "by value" - it isn't!  But "reference"
has a different meaning than in C++, where it's a sugar coated pointer.
In Python, assignment binds a name, in the present namespace, to
the indicated value.

There isn't any way to get what you want, that notation.  Depends on
your application, but the simplest thing would probably be to make only
explicit references to other modules, always "module1.a" and never "a".
That's easier to understand years later, anyway.

| Another question:
|
| module1:
| 	import module2
|
| module2:
| 	If I do: "python module1", How to call a function from module1, from
| here (module2)?

Kind of awkward to do that, isn't it?

I use module3, with a shared class instance.  module3 has just this
one class and this one module level instantiation of it.  All modules
import module3, and poke stuff into that instance - functions, global
configuration values, whatever needs to be shared across modules.
I imagine there may be package features that could be exploited, instead.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list