Python: dividiendo una función y argumentos

Here are some simple function calls in python:

foo(arg1, arg2, arg3)

Assume it is a valid function call.

Suppose I read these lines while parsing a file.

What is the cleanest way to separate the function name and the args into a list with two elements, the first a string for the function name, and the second a string for the arguments?

Resultados deseados:

["foo", "arg, arg2, arg3"]
["func1", ""]

I'm currently using string searches to find the first instance of "(" from the left side and the first instance of ")" from the right side and just splicing the string with those given indices, but I don't like how I am approaching the problem.

preguntado el 10 de marzo de 12 a las 08:03

2 Respuestas

I'm currently doing something similar using regular expressions. Adapting my code to your case, the following works with the examples you provide.

import re

def explode(s):
    pattern = r'(\w[\w\d_]*)\((.*)\)$'
    match = re.match(pattern, s)
    if match:
        return list(match.groups())
        return []

respondido 10 mar '12, 08:03

Oh cool, it handles input with nested parentheses as well. How does it do the parenthesis matching? - MXLDevs

It doesn't. It's equivalent to your current implementation, in that it just picks the last parenthesis on the line. - Amber

Proper parentheses matching needs something more powerful than regular expressions to be done. - Julio Piancastelli

If you're parsing a Python file in Python, consider using Python's parser: ast (específicamente el ast.parse() llamada).

That said, your current approach isn't terrible (though it will break on function calls that spam multiple lines). There are few completely correct approaches short of the aforementioned full parser - for instance, you could count matching parens, so that a((b,c)) would return the correct value even if there was a line break in the middle - but then that code would probably do the wrong thing when faced with a((b, "c)")), Y así sucesivamente.

respondido 10 mar '12, 08:03

Thanks for the suggestion. Though, it is not actually a python file; I just couldn't think of an easy way to describe the problem. It is a generic problem dealing with strings of the form "name(values)" where you are interested in separating the name from the values. - MXLDevs

Then I suppose it depends - do your values allow things like strings with parentheses in them? If they don't, then the problem is much simpler, but if they do, you start having to keep track of state and are effectively re-writing the same parsing logic as the language uses. - Amber

It doesn't look like any of the inputs I can be expecting contain parentheses, unless the user deliberately chooses wrong inputs. Which in that case I can just yell at them for not following the format. - MXLDevs

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.