Search code examples
pythonlistsolidityweb3pybrownie

web3.py and brownie: why does Contract.Struct_array(n) work but Contract.Struct_array[n] doesn't?


Inside Contract.sol I have defined

struct id {
   string name;
   uint num;
}

id[] public id_array;
.
.
.

In a python file, let C_adrs be the address of a deployed instance of Contract.sol. I want to print the 0th entry of the id_array of the deployed instance. For some reason, the following gives the desired result.

print(C_adrs.id_array(0))

On the other hand, the following gives 'ContractCall' object is not subscriptable.

print(C_adrs.id_array[0])

What is going on? I thought square brackets should access array entires.


Solution

  • well, yes in python with [] you can access to an array index, but this is a function not an array, at least for python, python don't know that id_array is an array since for it is a function that receive a parameter and returns something that it gets from the blockchain, in fact if you look at the abi of your contract and check what is id_array it will be a function that receive an uint256 and return an uint256