Alternatives to using eval()

Hi, I’m new to python, so this might be a simple problem, but i couldn’t find answer by searching. I want a function to return keyboard input for any given key. I couldn’t add argument into code, so i added it to string. But because eval() is a potential security risk, i would prefer not using it. Is there a better way to get the same result?:o

import bge

def button(key):
    return eval('bge.logic.keyboard.events[bge.events.' + key + ']')

if (button('WKEY')) == 1:
    print('foward')
if (button('SKEY')) == 1:
    print('backward')
if (button('AKEY')) == 1:
    print('left')
if (button('DKEY')) == 1:
    print('right')

Try this:

def button(key):
    return bge.logic.keyboard.events[getattr(bge.events, key)]

Thanks! :slight_smile:
getattr() does exactly what i needed.