class - How to declare variables to be used in multiple modules in python -


this question has been asked before, other posts found on stack overflow dealt changing global variables , importing functions etc. perhaps wording wasn't right when did quick search.

i starting code in python, , wondering efficient , proper way following is.

i have parent module importing other modules. has structure:

# import statements go here  # constants need same objects = 1 b = 2 ...  # class useful functions class def():     def __init__(self):     ... 

i importing module in others, use functions in def class. want modules importing have access constants declared outside class.

i declared them outside, want values used wherever called them in module. did not want attach them objects (and have refer them self.a, self.b etc.) , constants throughout every module using.

some of options considering are:

  1. declare these global variables in separate class, , import class in every module
  2. use from parentmodule import *

i not sure if either of these work, , if there standard way of doing this. conflicts in names of variables while doing latter not problem, because of nature of constants. in context of program, physical constants unique names not used represent other variables.

apologies if question basic/previously asked, , thank help.

importing whole module act sort of namespace, , seems me simplest way it. have "mystuff.py":

a = 1 b = 2  def myfunc():     .... 

then in other modules:

import mystuff  print mystuff.a print mystuff.b  mystuff.myfunc() 

your solution to:

from mystuff import * 

works, possibly conflicts , i'm thinking isn't people consider "pythonic."


Comments