0

I know there have been several posts about this, but I am still confused. Am trying to use a static variable with initialization, and don't know how to do it. So what I have is a package 'config', which has a module the_config.py. What I would like is for this to be something like

# the_config.py
import yaml
user_settings=None
def initialize(user_settings_file)
 with open(user_settings_file) as yaml_handle:
 user_settings = yaml.safe_load(user_settings_file)

Then there would be a calling module as pipeline.py

#pipeline.py
import config.the_config as my_config
def main(argv):
 ...
 my_config.intialize(user_settings_file)
 print my_config.user_settings['Output_Dir']

But this doesn't work. How should I be doing this please?

Thanks in advance.

asked Sep 23, 2013 at 17:24

3 Answers 3

3

When you assign to user_settings, it is automatically treated as a local variable in the initialize function. To tell Python that the assignment is intended to change the global variable instead, you need to write

global user_settings

at the beginning of initialize.

answered Sep 23, 2013 at 17:26
Sign up to request clarification or add additional context in comments.

Comments

0

In Python any variable that is assigned in the body of a function is considered a local variable, unless it's has been explicitly declared differently with either global or nonlocal declarations.

Python considers also assignment any "augmented-assignment" operator like += or /=.

The mandatory declaration of global that are modified is a (little) price to pay to the fact that in Python there is no need to declare variables.

It's also assumed that your code doesn't rely too much on mutating state in that is kept global variables so if your code requires a lot of global declarations then there's probably something wrong.

answered Sep 23, 2013 at 17:35

1 Comment

I just want to do this for a few things - like config and log settings. So initialize them once, and then use them throughout all the classes
0

I can propose You some way to solve this.

First of all the root of your problem is creation of new local variable in your initialize function

user_settings = yaml.safe_load(user_settings_file)

As soon as there is equal sign right to variable name python create new variable in corresponding scope (in this case local for initialize function

to avoid this one can use following:

use global declaration

def initialize(user_settings_file)
 global user_settings # here it is
 with open(user_settings_file) as yaml_handle:
 user_settings = yaml.safe_load(user_settings_file)

modify existing variable but not create new one

user_settings = {}
def initialize(user_settings_file)
 with open(user_settings_file) as yaml_handle:
 user_settings.update(yaml.safe_load(user_settings_file)) # here we modify existing user_settings

operate with module attribute (this one is quite tricky)

user_settings = {}
def initialize(user_settings_file)
 with open(user_settings_file) as yaml_handle:
 import the_config
 the_config.user_settings = yaml.safe_load(user_settings_file)
answered Sep 23, 2013 at 17:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.