Sometimes you need a more complex data type. For this, Vyper provides structs
:
struct Person:
age: uint256
name: String[64]
Structs allow you to create more complicated data types that have multiple properties.
Note that we just introduced a new type, String. Fixed-size Strings can hold strings with equal or fewer characters than the maximum length of the string.
testString: String[100] = "Hello World!"The above
testString
can hold upto 100 characters. We have only used 12 characters in"Hello World!"
Another thing to note is that Vyper (like Python) uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right.
Taking the Person
struct above as an example, age
and name
are indented to same distance to the right. Hence they belong to the same block of code.
In our app, we're going to create some Pokemons! And Pokemons will have multiple properties, so this is a perfect use case for a struct.
-
Create a
struct
namedPokemon
. -
Our Pokemon struct will have 5 properties:
name
(String[32]
)dna
(uint256
)HP
(uint256
)matches
(uint256
)wins
(uint256
)