Reputation: 199
Say that I have a function like the following:
def eggs(a,b,c):
if c.foo:
return a + b.bar
else:
return c.spam
I'd like to have a higher order function capable of introspecting the function passed and retrieve what members of an argument are mentioned inside the code via the dot syntax, with the following behavior:
>>> member_inspector(eggs, 'c')
('foo','spam')
>>> member_inspector(eggs, 'b')
('bar')
>>> member_inspector(eggs, 'a')
()
Can this be done? How?
Upvotes: 3
Views: 70
Reputation: 36013
Here's a basic version:
import inspect
from textwrap import dedent
import ast
def member_inspector(f, var):
source = dedent(inspect.getsource(f))
module = ast.parse(source)
func = module.body[0]
result = []
for stmt in func.body:
for node in ast.walk(stmt):
if (isinstance(node, ast.Attribute) and
isinstance(node.value, ast.Name) and
node.value.id == var):
result.append(node.attr)
return result
Upvotes: 5