0

I have the following code inside of a file called my_python_script-

def write_output(timestamp, name, alias, key, value, component, source, env):
    """ This is generic Function to write the output of the other scripts"""

    metrics_file = "/opt/reporting/data/metrics"
    data = "writing some stuff"
    with open(metrics_file, "a") as myfile:
        myfile.write(data)

When I do import my_python_script, I can successfully call the function from a separate script after I've imported it.

Is there a way to make it so that I can change the value of "metrics_file" in the second script that I am importing my_python_script into? I am trying to make it so that I can just overwrite the metrics_file variable and continue to use the same function?

Thanks.

6
  • 1
    how about make the metrics_file var as another argument of the function ? Commented Jun 12, 2017 at 20:41
  • 1
    Why don't you make it a parameter of the write_output function? Commented Jun 12, 2017 at 20:41
  • 1
    make a metrics_file as a function parameter Commented Jun 12, 2017 at 20:44
  • 2
    Nope, that's impossible unless you make metrics_file that function's argument. Commented Jun 12, 2017 at 20:45
  • 2
    you can make the parameter optional with def write_output(timestamp, name, alias, key, value, component, source, env, metrics_file = "/opt/reporting/data/metrics"): , it will default to "/opt/reporting/data/metrics" Commented Jun 12, 2017 at 20:47

1 Answer 1

2

A simple solution is to make the filename an optional parameter:

def write_output(timestamp, namename, alias, key, value, component, source,
                 env, metrics_file="/opt/reporting/data/metrics"):

This will by default use the same filename as you have before but also allows you to call it with a different filename.

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

Comments

Your Answer

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.