Back
Lesson 6:
Arrays
Introduction to Vyper arrays
Progress: 0%
Visit desktop version for better experiences.
Arrays
Arrays can be defined with a fixed size at compile time, or they can be dynamically sized.
Fixed size arrays are declared by specifying size k
in data type T
. This is written as T[k]
.
# pragma version 0.4.0
# Define array with fixed size
fixed_array: uint256[5]
@external
def setFixedArray():
self.fixed_array = [1,2,3,4,5]
@external
@view
def getFixedArray() -> uint256[5]:
return self.fixed_array
ℹ️ Note
Avoid defining an array with size larger than
2**64
as this can result in security vulnerabilities due to risk of overflow.
Dynamic arrays length can be modified at runtime, up to a bound specified in the type declaration. This is written as dynArrayVariable: DynArray[T, k]
.
# pragma version 0.4.0
# Notice you will need to define the size of the dynamic array using `DynArray` keyword
dynamicArrayList: DynArray[uint256, 5]
@deploy
def __init__():
self.dynamicArrayList.append(100)
self.dynamicArrayList.append(200)
self.dynamicArrayList.append(300)
self.dynamicArrayList.append(400)
self.dynamicArrayList.append(500)
# In this example, appending more than 5 elements would revert
@external
@view
def getDynamicArray() -> uint256[5]:
return self.dynamicArrayList
Vyper Differentiators
- Requires explicit specification of size of arrays at compile time.
- This applies to both fixed and dynamic array.