for loop over function that returns a tuple?
Jussi Piitulainen
harvesting at makes.address.invalid
Wed Sep 2 11:50:02 EDT 2015
Steven D'Aprano writes:
> On Wed, 2 Sep 2015 09:49 pm, Victor Hooi wrote:
>
>> 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 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 don't understand this.
Like so:
for metric_data in (timestamp, "serverstatus", values, tags):
foo(*metric_data)
Because get_metrics returns a single tuple.
If it's really essential to "loop" just once, perhaps wrap the one tuple
in a list:
for metric_data in [get_metrics(server_status_json, mmapv1_metrics,
line_number)]:
json_points.append(create_point(*metric_data))
More information about the Python-list
mailing list