Dictionary class¶
This page explains the Dictionary class.
Before reading on, maybe it is helpful to read the following page:
What is the Dictionary?¶
The Dictionary class is the apysc dictionary class. It behaves like the Python built-in dict value.
Constructor method¶
The Dictionary class constructor method requires a Python built-in dict or Dictionary value:
import apysc as ap
ap.Stage()
dict_1: ap.Dictionary = ap.Dictionary({"a": 10})
assert dict_1 == {"a": 10}
dict_2: ap.Dictionary = ap.Dictionary(dict_1)
assert dict_1 == dict_2
Value setter interface¶
A Dictionary value can be updated by indexing, like the Python built-in dict value:
import apysc as ap
ap.Stage()
dict_1: ap.Dictionary = ap.Dictionary({"a": 10})
dict_1["a"] = 20
assert dict_1 == {"a": 20}
Value getter interface¶
A Dictionary value also can be retrieved by indexing:
import apysc as ap
ap.Stage()
int_1: ap.Int = ap.Int(10)
dict_1: ap.Dictionary = ap.Dictionary({"a": int_1})
int_2: ap.Int = dict_1["a"]
assert isinstance(int_2, ap.Int)
assert int_2 == 10
Notes of the getter interface¶
If a Dictionary value doesn’t have the specified key, a retrieved value type becomes the AnyValue type. This behavior occasionally is helpful when a Dictionary value is updated dynamically (e.g., updating by the JavaScript event handler).
import apysc as ap
ap.Stage()
int_1: ap.Int = ap.Int(10)
dict_1: ap.Dictionary = ap.Dictionary({"a": int_1})
retrieved_val: ap.AnyValue = dict_1["b"]
assert isinstance(retrieved_val, ap.AnyValue)
Value deletion interface¶
A Dictionary value can be deleted by the del statement, as follows:
import apysc as ap
ap.Stage()
int_1: ap.Int = ap.Int(10)
dict_1: ap.Dictionary = ap.Dictionary({"a": int_1})
del dict_1["a"]
assert dict_1 == {}
Dictionary class constructor API¶
Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.
[Interface signature] __init__(self, value: Union[Dict[~_Key, ~_Value], ForwardRef('Dictionary')], *, variable_name_suffix: str = '', skip_init_substitution_expression_appending: bool = False) -> None
[Interface summary]
Dictionary class for the apysc library.
[Parameters]
value: dict or DictionaryInitial dictionary value.
variable_name_suffix: str, default “”A JavaScript variable name suffix string. This setting is sometimes useful for JavaScript debugging.
skip_init_substitution_expression_appending: bool, default FalseA boolean indicates whether to skip an initial substitution expression or not. This class uses this option internally.
[Examples]
>>> import apysc as ap
>>> _ = ap.Stage()
>>> dictionary: ap.Dictionary = ap.Dictionary({"a": 10})
>>> dictionary
Dictionary({'a': 10})
>>> dictionary["a"]
10
>>> dictionary["b"] = 20
>>> dictionary["b"]
20
[References]
value attribute API¶
Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.
[Interface summary]
Get a current dict value.
[Returns]
value: dictCurrent dict value.
[Examples]
>>> import apysc as ap
>>> _ = ap.Stage()
>>> dictionary: ap.Dictionary = ap.Dictionary({})
>>> dictionary.value = {"a": 10}
>>> dictionary.value
{'a': 10}
[References]