Friday 15 March 2013

python - Passing a list to eval() -


There is a way to pass a list as function argument for eval () or do I convert it to string And then parse it as a list in the function?

My simple example looks like this:

  eval ("func1 (\ '+ + + + +" \ "))  
< P> I'm not sure that there is a better way of taking fords as a list than a string instead

Note: The list is given from JSON

Edit: Ok here's my There is a bit more of the class, there is a better understanding of how I am using eval

  def test (arg): print arg # add to map all the functions in this list Dictionary to secure_link = ['tess Secure_dict = dec ([(k, local). For safely (for the list) for Kashmir (k, none)) Valid values: def __init __ (self, value, f, name, frags): eval (Fname + "(\ '" + fArgs + "\')", {"__builtins __": none}, safe_dector)  

It is wrong to think, but in my opinion This is a safe use Eval because Functions that can be told are those that are listed in the Safe_link dictionary Funk to run Action and rationale for the function to be removed from the JSON object. Arguments have to be structured in the form of a list, with the joining in the list "," can be interpreted as a real argument or just an argument?

If you are using Python 2.6.x, you will see the Json Module (see py doc). If not, the Python package is available through the index. Both of these packages will provide a reader to parse JSON data in the appropriate Python data type.

For your second problem of calling a function defined by a message, you can do the following:

  def foo (): print 'i foo!' Def bar (): pass def baz (): pass funcs = {'func_a': foo, 'func_b': bar, 'func_c': baz} funcs ['func_a'] ()  
< P> This approach can be a bit safer than eval because it prevents 'unsafe' python library functions from being in JSON. However, you still need to be cautious that the data given to your function can not be manipulated to cause problems.


No comments:

Post a Comment