What do _ast Load | Store | Del | AugLoad | AugStore | Param mean?

E

Eloff

In the _ast module Attribute, Subscript, Name, List, Tuple all have an
expr_context associated with them which is defined as:

expr_context = Load | Store | Del | AugLoad | AugStore | Param

I have no idea what they mean, and what's the difference between them.
I'm porting _ast to IronPython and this is the only part I've had
difficulty understanding. The only clue I've found is that all these
expressions are unique in that they can occur in assignment context.

Any insights at all?

Thanks,
-Dan
 
B

Benjamin

In the _ast module Attribute, Subscript, Name, List, Tuple all have an
expr_context associated with them which is defined as:

expr_context = Load | Store | Del | AugLoad | AugStore | Param

I have no idea what they mean, and what's the difference between them.
I'm porting _ast to IronPython and this is the only part I've had
difficulty understanding. The only clue I've found is that all these
expressions are unique in that they can occur in assignment context.

Any insights at all?

They refer to how the expression is used in its context. You can cross
AugLoad and AugStore off because they are not used by the current
implementation. I hope this code examples will explain the rest:

Load:
a = 2
Module(body=[Assign(targets=[Name(id='a', ctx=Store())],
value=Num(n=2))])

Store:
print a
Module(body=[Print(dest=None, values=[Name(id='a', ctx=Load())],
nl=True)])

Del:
del a
Module(body=[Delete(targets=[Name(id='a', ctx=Del())])])

Param:
def f(a): pass
Module(body=[FunctionDef(name='f', args=arguments(args=[Name(id='a',
ctx=Param())], vararg=None, kwarg=None, defaults=[]), body=[Pass()],
decorator_list=[])])

I hope that helps!
 
E

Eloff

Load:
a = 2
Module(body=[Assign(targets=[Name(id='a', ctx=Store())],
value=Num(n=2))])

Store:
print a
Module(body=[Print(dest=None, values=[Name(id='a', ctx=Load())],
nl=True)])

Del:
del a
Module(body=[Delete(targets=[Name(id='a', ctx=Del())])])

Param:
def f(a): pass
Module(body=[FunctionDef(name='f', args=arguments(args=[Name(id='a',
ctx=Param())], vararg=None, kwarg=None, defaults=[]), body=[Pass()],
decorator_list=[])])

I hope that helps!

Very much! Thank you for such a clear explanation!

Cheers,
-Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top