prodigy.pgcl.util.ref¶
Mutable References¶
- class Mut[M](read, write)[source]¶
Bases:
Generic[M]A mutable reference to a value of type M, represented a getter and a setter. Use
valto read and write the value.Mut is useful to transform an AST while walking it.
- property val: M¶
Property that wraps
readandwrite.
- static alloc(value)[source]¶
Create a new Mut in a new anonmyous location.
- Parameters:
value (
M)- Return type:
Mut[M]
- static list(values)[source]¶
Create a Mut for each element in the list.
>>> my_list = [1, 2, 3] >>> ref_list = list(Mut.list(my_list)) >>> ref_list[0].val = 15 >>> my_list [15, 2, 3]
- static dict_items(values)[source]¶
Create a Mut for each key-value pair in the dict.
>>> my_dict = {'hello': 42} >>> refs = list(Mut.dict_items(my_dict)) >>> refs[0][1].val = 43 >>> my_dict {'hello': 43}
- static dict_values(values)[source]¶
A Mut reference to each value in the map. See
dict_items().
- static tuple(values)[source]¶
A Mut reference to each value in the tuple. Note that tuples are immutable, meaning assignment to its items will result in an exception.
>>> my_tuple = ('hello', 42, 3.14) >>> refs = list(Mut.tuple(my_tuple)) >>> [mut.val for mut in refs] ['hello', 42, 3.14] >>> refs[1].val = 43 Traceback (most recent call last): ... TypeError: 'tuple' object does not support item assignment
- Ref = prodigy.pgcl.util.ref.Mut | ~M¶
A Ref is either a Mut or a value.