Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Import and variable level
Hi, I am working with a few friends on a project where everyone makes some functions that have to use the same variables, for example x here. Every function will be imported in a file and then use x, which is created in We thought that it would not be necessary to put x as an argument of every function because x would be a global variable, but it seems like it is not. Is there a solution to make this work, other than storing every variables in a file and importing this file everywhere ? Or is there a cleaner way to use our functions together ? We would like to avoid putting the variables as arguments of the functions because there is a lot of them.
def function():
    print("secondary:", x)
from secondary import function
I get
NameError: name 'x' is not defined
and I want to print "secondary: 0"
you need to pass x as an argument:
def function(x):
    print("secondary:", x)
from secondary import function
Also, be aware that you set x to 0 prior to running function

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  python import module or namespace from namepsace variable ? harun2525 11 3,840 May-23-2017, 05:39 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)