擅长:python、mysql、java
<p>您可以使用<a href="https://docs.python.org/2/library/ast.html" rel="nofollow">ast</a>模块。在</p>
<pre><code>import ast
my_code = """
print "Hello"
y = [x ** 2 for x in xrange(30)]
"""
module = ast.parse(my_code)
for node in ast.walk(module):
if type(node) == ast.ListComp:
print node.lineno # 3
print node.col_offset # 5
print node.elt # <_ast.BinOp object at 0x0000000002326EF0>
</code></pre>