0

I am importing one module from another module

1st module has

a variable initialized to None
a method

In another module I am importing the first module using

from 1stmodule import method

but this is initializing the variable again to None when I import, how can I import only the method to second module without changing the variable?

1
  • What do you mean by " initializing the variable again to None" ? Importing the same module a second time in a same process won't reinitialize the module. Commented Dec 19, 2013 at 16:39

2 Answers 2

1

You would need to use the global keyword e.g. global your_var to have any changes you make to it avaiable globally. See this question for more details.

You would also need to be exceedingly careful how and where you modify this variable because if you lose track, you're going to waste a lot of time debugging and wondering why the variable isn't coming back with the value you expected.

There's always a debate about just how dangerous global variables are, but I tend to be of the opinion- If you're not sure whether to use a global variable or not, then you shouldn't be using a global variable.

In reality I suggest modifying your approach.

Sign up to request clarification or add additional context in comments.

6 Comments

What does the global statement has to do with the op's problem ?
He's attempting to globally modify the variable in 1stmodule, which won't work as it isn't a global variable. See this similar question for a deeper explaination.
we dont see his code so we dont know what he is trying to do, and the global statement is a noop at module level - it's only meaningful within a function body.
You're right in that we don't know for sure what he's trying to do without seeing his code. It's not too much of a stretch to imagine that he's after a global variable however, when you take into account that he's modifying it and attempting to access it from other modules. And I can think of no other reason why you would have a module level variable set to None if you had no intention of ever modifying it.
as long we don't see the code everything is just wild guess - specially the fact that he would be "attempting to access it from other modules" (the fact that he just wants to import the function going against your assumption FWIW). But anyway, until the OP gives more details there's just no possible "good answer" IMHO.
|
1

When you import a module, Python will execute the the module's code. That's why you can execute a script by simply importing it. To prevent the module from being executed, add:

if "__name__" == __main__:
    # the code that should run if this file
    # is run directly as a script, e.g. from
    # the command line
    main()

This statement tells Python: Execute main(), but only if I (the module) am not being imported.

So, since you have the variable initialization at global scope in your first module, it gets executed when importing that module.

And as @brunodesthuilliers saids: Top level module statements are only executed on the first import (for a given process)

6 Comments

Yes, correct answer here.
I'm not sure that I agree that it's an initialisation problem. As @bruno_desthilliers pointed out, the module only gets imported once. From what I can gather from the OP he was having difficulty understanding why the module level variable that he initially set to None and (I assume) changed the value of somewhere in 1stmodule was back to None again when he tried to access it directly. This isn't caused by the module "re-initialising" on import.
Sorry alice but that's not "the correct answer". Top level module statements are only executed on the first import (for a given process).
@brunodesthuilliers Ok, I'm editing the answer right now. But the execution of that top level module statement is the reason of the variable initialization to None.
@RaydelMiranda : yes, "execution of that top level module statement is the reason of the variable initialization to None", but that's only happening on the first import. Unless the OP expects to have one process updating the variable and another one being able to access the updated value (which is so far the only way I can understand his question, but that's really a wild guess), the fact that top level statements are executed on first import is irrelevant - you'd get the very same result with most languages, whatever the execution model.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.