What is an alternative to eval in this situation?

Posted by CppLearner on Stack Overflow See other posts from Stack Overflow or by CppLearner
Published on 2012-04-08T05:26:19Z Indexed on 2012/04/08 5:29 UTC
Read the original article Hit count: 131

Filed under:
|
|

Many of my view functions do similar things. For the most part, they reverse to a different views upon clicking a button / a text link.

So I wrote a helper function render_reverse

def render_reverse(f, args):  # args are all string type
    return eval('reverse(' + f + ', ' + args + ')' )

eval is a bad practice, and is pretty slow. It takes 3 seconds to start redirecting, whereas calling reverse directly takes less than 1 second to start redirecting.

What alternative do I have? By the way, the function above doesn't work properly. I was modelling after this line (which works)

eval('reverse("homepage", args=["abcdefg"])')

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about django