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?

  • 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. – bruno desthuilliers Dec 19 '13 at 16:39

2 Answers2

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.

Community
  • 1
  • 1
ptr
  • 3,292
  • 2
  • 24
  • 48
  • What does the `global` statement has to do with the op's problem ? – bruno desthuilliers Dec 19 '13 at 16:34
  • 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](http://stackoverflow.com/questions/7060711/accessing-module-level-variables-from-within-a-function-in-the-module) for a deeper explaination. – ptr Dec 19 '13 at 16:53
  • 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. – bruno desthuilliers Dec 19 '13 at 17:32
  • 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. – ptr Dec 19 '13 at 19:19
  • 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. – bruno desthuilliers Dec 19 '13 at 21:40
  • Before I got these comments I left stackoverflow, It is a client server program, More than one client will request for a web service in server, and the above two modules are at server side only and after some period of time, the server will acquire some value for that variable and after that if the client sends a request to server again, the new request taking the variable value as again None (means again reinitializing to None). This is because at server in second module I am importing the 1st module which is reinitializing the variable value to None. Thanks for the answers given. – user3045128 Dec 20 '13 at 05:11
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)

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • Yes, correct answer here. – Alice Dec 19 '13 at 17:03
  • 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. – ptr Dec 19 '13 at 17:13
  • Sorry alice but that's not "the correct answer". Top level module statements are only executed on the first import (for a given process). – bruno desthuilliers Dec 19 '13 at 17:38
  • @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. – Raydel Miranda Dec 19 '13 at 18:09
  • @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. – bruno desthuilliers Dec 19 '13 at 21:47
  • @brunodesthuilliers: I'm agree with you. – Raydel Miranda Dec 19 '13 at 21:49