1
2
3
4
5
6
7
8
9
10
11
12
13
14
import ast
class Visitor(ast.NodeVisitor):
def visit_Attribute(self, node):
print(ast.dump(node, include_attributes=True))
return self.generic_visit(node)
code = "foo.bar()"
tree = ast.parse(code)
visitor = Visitor()
visitor.visit(tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
The script outputs (added indent):
Attribute(
value=Name(
id='foo',
ctx=Load(),
lineno=1,
col_offset=0
),
attr='bar',
ctx=Load(),
lineno=1,
col_offset=0
)
attribute's col_offset is 0.
How can I find the offset of the attribute "bar" in the code?