mardi 22 décembre 2020

Can I retrieve already parsed definition of function?

Using inspect.getsource you can retrieve definition / source code of a function in Python. Is there a way to retrieve that definition but as an already parsed object?

In other words, instead of a string, I'd like to get a list of some objects, each object represents one line of code and that object stores for example the function that is called at that line and all of the parameters of that function etc. So I'd expect that object to be a tree.

For example a line like that:

a = b(c, d(5))

would be a tree in which the root is "=" operator, below that is variable "a" and function "b", below function "b" is variable "c" and function "d", below function "d" would be literal 5.

In other words, I need to inspect the definition of a function, but with inspect.getsource, I would need to parse the returned string first so that I can work with that. Is there any way in which I can avoid doing that parsing and have it already parsed?





Aucun commentaire:

Enregistrer un commentaire