web3.py 和布朗尼:为什么 Contract.Struct_array(n) 可以工作,而 Contract.Struct_array[n] 却不能?
在 Contract.sol
内部,我在一个 python 文件中定义了
struct id {
string name;
uint num;
}
id[] public id_array;
.
.
.
C_adrs
为 Contract.sol
已部署实例的地址。我想打印已部署实例的 id_array 的第 0 个条目。由于某种原因,以下给出了所需的结果。
print(C_adrs.id_array(0))
另一方面,以下给出了'ContractCall'对象不可订阅
。
print(C_adrs.id_array[0])
到底是怎么回事?我认为方括号应该访问整个数组。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,是的,在 python 中使用 [] 可以访问数组索引,但这是一个函数而不是数组,至少对于 python 来说,python 不知道 id_array 是一个数组,因为它是一个接收参数的函数并返回从区块链获取的内容,事实上,如果您查看合约的 abi 并检查 id_array 是什么,它将是一个接收 uint256 并返回 uint256 的函数
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