for loop over function that returns a tuple?

Victor Hooi victorhooi at gmail.com
Wed Sep 2 13:49:21 CEST 2015


I have a function which is meant to return a tuple:

    def get_metrics(server_status_json, metrics_to_extract, line_number):
        <SOME_CODE>
        return ((timestamp, "serverstatus", values, tags))

I also have:

    def create_point(timestamp, metric_name, values, tags):
        return {
            "measurement": _MEASUREMENT_PREFIX + metric_name,
            "tags": tags,
            "time": timestamp,
            "fields": values
        }

I am calling get_metric in a for loop like so:

    for metric_data in get_metrics(server_status_json, mmapv1_metrics, line_number):
        json_points.append(create_point(*metric_data))

I was hoping to use tuple unpacking to pass metric_data straight from get_metrics through to create_point.

However, in this case, metric_data only contains timestamp.

I suppose I could assign multiple variables like, and pass them through:

    for timestamp, metric_name, value, tags in get_metrics(server_status_json, common_metrics, line_number):

However, that seems unnecessarily verbose, and I'm sure there's a simple way to do this with tuple unpacking?


More information about the Python-list mailing list