Array class

This page explains the Array class.

Before reading on, maybe it is helpful to read the following page:

What is the Array?

The Array class is the apysc array class. It behaves like the Python built-in list value.

Constructor method

The Array class constructor method requires iterable objects, like the list, tuple, range, or Array value.

import apysc as ap

ap.Stage()
arr_from_list: ap.Array = ap.Array([1, 2, 3])
assert arr_from_list == [1, 2, 3]

arr_from_tuple: ap.Array = ap.Array((4, 5, 6))
assert arr_from_tuple == [4, 5, 6]

other_arr: ap.Array = ap.Array([7, 8, 9])
arr_from_arr: ap.Array = ap.Array(other_arr)
assert arr_from_arr == [7, 8, 9]

Generic type annotation

If the Array values types are unique, you can set the generic type to an Array value. This annotation may be helpful when you use it on the IDE (for type checkers).

import apysc as ap

ap.Stage()
arr: ap.Array[int] = ap.Array([1, 2])
int_val: int = arr.pop()
assert isinstance(int_val, int)

fixed_value_type argument

The constructor’s fixed_value_type argument is optional.

If specified, the array value-related interfaces, such as the __getitem__ method (array subscript interface, for example, arr[5]), becomes possibly returning a specified type instance.

It returns an instance of the specified type, especially when an array index exceeds the length of an array on the Python runtime.

import apysc as ap

ap.Stage(
    stage_width=100,
    stage_height=50,
    background_color=ap.Color("#333"),
    stage_elem_id="stage",
)
arr: ap.Array[ap.Int] = ap.Array([ap.Int(10)], fixed_value_type=ap.Int)

# Index 5 is out of bounds on the Python runtime,
# but since the `fixed_value_type` is the `ap.Int`,
# this interface returns an `ap.Int` value.
int_value: ap.Int = arr[5]
assert isinstance(int_value, ap.Int)

See also

Array 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[List[~_ArrValue], tuple, range, ForwardRef('Array')], *, fixed_value_type: Union[Type[apysc._loop.initialize_with_base_value_interface.InitializeWithBaseValueInterface], NoneType] = None, variable_name_suffix: str = '', skip_init_substitution_expression_appending: bool = False) -> None


[Interface summary]

Array class for the apysc library.


[Parameters]

  • value: Array or list or tuple or range

    • Initial array value.

  • fixed_value_type: Optional[Type[InitializeWithBaseValueInterface]], optional

    • A fixed value type of array. This argument only becomes an apysc type, such as the ap.Int, ap.String, or ap.Rectangle. If specified, the array value-related interfaces, such as the __getitem__ method (array subscript interface, for example, arr[5]), becomes possibly returning a specified value type instance.

  • 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 False

    • A boolean indicates whether to skip an initial substitution expression or not. This class uses this option internally.


[Examples]

>>> import apysc as ap
>>> _ = ap.Stage()
>>> arr: ap.Array = ap.Array([1, 2, 3])
>>> arr
Array([1, 2, 3])

>>> arr[0]
1

>>> arr[1]
2

>>> arr = ap.Array((4, 5, 6))
>>> arr
Array([4, 5, 6])

>>> arr = ap.Array(range(3))
>>> arr
Array([0, 1, 2])

[References]

value property 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 array value.


[Returns]

  • value: list

    • Current array value.


[Examples]

>>> import apysc as ap
>>> _ = ap.Stage()
>>> arr: ap.Array = ap.Array([1, 2, 3])
>>> arr.value = [4, 5, 6]
>>> arr.value
[4, 5, 6]

[References]