Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python: call functions whose names are stored in variables

$
0
0

I am developing an application with check the status of a bunch of sensors.

The way to get their status is very straightforward:

sensor_name.get_status()

Instead of writing several calls to check all those sensors, I was wondering if I could store all sensor names in a list of strings and let a for do the work for me.

Pretty much like this:

sensors_list = ['sensor_name_1', 'sensor_name_2', 'sensor_name_3'] for sensor in sensors_list: #call it :-)

How can I get this done?

How about, instead of keeping the names in the list, you just keep the sensors themselves in the list?

sensors = [sensor_1, sensor_2, sensor3] for sensor in sensors: sensor.get_status()

Viewing all articles
Browse latest Browse all 9596

Trending Articles