Hello,

Yes, using a dictionary is the best way to do this, because it offers a
direct translation from a string to the function, and is very extensible.



On Sun, Oct 2, 2011 at 10:50 AM, <sam.hack...@sent.com> wrote:

>  Hi,
>
>  In my GUI toolkit, I have config files that the program can parse and turn
> into a complete menu screen. Now, if I have a button in my menu, and I want
> that button to run a function when it is clicked, then I need some way of
> naming a function from the config file.
>
>  At the moment I am just parsing the function name and running it through
> eval() so it is translated into a real function. For example, a button may
> have it's function set to "exit", which evaluates into Python's exit()
> function when parsed.
>
>  This has always felt a bit awkward to me, and now it seems that this
> method doesn't work when a project is compiled with PyInstaller. So, I was
> wondering if there was a well known solution to this problem?
>
>  The first thing I've thought of is accessing it through a dictionary. So,
> if I instance a Menu class, and then give it a dictionary that uses a string
> as a key, and the approriate function as a value. Then I can just index this
> dictionary with the string parsed from the config file, in order to return a
> function.
>
>  If anybody can provide any feedback on this, or any better methods for
> achieving this, it would be greatly appreciated.
>
>  Thanks,
>  Sam Bull
>

Reply via email to