[Tutor] Pseudo-functions and dicts

Shrutarshi Basu technorapture at gmail.com
Sat Mar 22 03:01:12 CET 2008


I have a dictionary (in a module) which contains values of various
sensors. I would like a user to be able use to a function syntax to
get the sensor values, both as a whole and individually. Currently I
have a function for each value who's sole purpose is to look up the
corresponding dict value and return. But I don't think this is an
elegant way to solve the problem. Is there someway I could define a
single function that responds to all of possible function calls?

Eg.
My dict : sensor = {'sens1': 200, 'sens2': 300} etc
my functions:
def sens1():
     return sensor['sens1']
same for sens2.

There are two solutions I've thought about:
Have a function that takes in the sensor's name as a string and
responds accordingly. (which might be what I'll end up using)
I could let the user directly access the dict, but I'm not sure if
that is a good idea. My project requires that the user of my module
should not have to know about Python's data structures to use the
values my module returns. If there is some sort of non-functional
dot-separated syntax that I could use, that would be good too.
Thanks,
Basu

-- 
Embedded Architectures, Systems programming and outrageous ideas --
http://xcomputers.wordpress.com


More information about the Tutor mailing list