web3.py 和布朗尼:为什么 Contract.Struct_array(n) 可以工作,而 Contract.Struct_array[n] 却不能?

发布于 2025-01-13 07:27:47 字数 451 浏览 2 评论 0原文

Contract.sol 内部,我在一个 python 文件中定义了

struct id {
   string name;
   uint num;
}

id[] public id_array;
.
.
.

C_adrsContract.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2025-01-20 07:27:47

好吧,是的,在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文