Python AST Attributes

Python AST Attributes are special properties assigned to specific nodes in Python’s Abstract Syntax Tree (AST). These attributes provide additional information about what’s happening at that point in the code. Here are some examples:

1) Line number and column position This attribute tells you exactly where a particular line of code appears in your script, making it easier to pinpoint any errors or issues. For example, if you see an error message that says “SyntaxError on line 23, column 10”, you know right away which part of the code needs attention.

2) Token type This attribute tells you what kind of token (i.e., keyword, operator, literal value) is being used in a particular line of code. For example, if you see “NAME ‘my_variable’”, that means the current node represents a variable name. If it says “ADD ‘+’”, then we’re dealing with an addition operation.

3) Operand type This attribute tells you what kind of operands (i.e., values being used in an expression) are involved in a particular line of code. For example, if you see “BINARY_OPERATION (‘+’, int(1), int(2))”, that means we’re dealing with an addition operation involving two integer values.

4) Keyword type This attribute tells you what kind of keyword (i.e., reserved word in Python) is being used in a particular line of code. For example, if you see “IF ‘if’”, then we’re dealing with the start of an IF statement.

5) Function call arguments If a function call appears in your code, this attribute will show you what kind of arguments (i.e., values being passed to the function) are involved. For example, if you see “CALL_FUNCTION (‘len’, [list(range(10))])”, that means we’re calling the len() function with a list as an argument.

6) Attribute reference This attribute tells you what kind of object (i.e., variable or class) is being referenced, and which specific attribute is being accessed. For example, if you see “ATTRIBUTE ‘my_object.my_attribute’”, that means we’re accessing a particular attribute within an object called my_object.

7) Function definition This attribute tells you what kind of function (i.e., named or anonymous) is being defined in your code. For example, if you see “FUNCTION_DEF ‘def my_function(x):’”, that means we’re defining a new function called my_function with one argument called x.

8) Class definition This attribute tells you what kind of class (i.e., named or anonymous) is being defined in your code. For example, if you see “CLASS_DEF ‘class MyClass(object):’”, that means we’re defining a new class called MyClass which inherits from the object class.

9) Import statement This attribute tells you what kind of import (i.e., module or package) is being used in your code. For example, if you see “IMPORT ‘import math’”, that means we’re importing the math module into our script.

10) Assignment expression This attribute tells you what kind of assignment operation (i.e., simple or augmented) is being performed in a particular line of code. For example, if you see “STORE ‘x = 5′”, that means we’re assigning the value 5 to a variable called x using a simple assignment operation. If it says “AUGMENT_ASSIGN ‘x += 1′”, then we’re performing an augmented assignment operation (i.e., adding one to the current value of x).

SICORPS