J. Doe
J. Doe

Reputation: 1529

Changing a function variable in a seperate Python script when it is imported

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.

Upvotes: 0

Views: 29

Answers (1)

Ben G.
Ben G.

Reputation: 306

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.

Upvotes: 2

Related Questions