python sizeof_Python的construct.sizeof()取决于它的paren

这篇文章是关于Python的Construct库

代码

以下是我的构造的定义:from construct import *

AttributeHandleValuePair = "attribute_handle_value_pair" / Struct(

"handle" / Int16ul,

"value" / Bytes(this._.length - 2)

)

AttReadByTypeResponse = "read_by_type_response" / Struct(

"length" / Int8ul, # The size in bytes of each handle/value pair

"attribute_data_list" / AttributeHandleValuePair[2]

)

问题

正在尝试运行以下命令:

AttReadByTypeResponse.sizeof(dict(length=4, attribute_data_list=[dict(handle=1, value=2), dict(handle=3, value=4)])

我收到以下错误:

^{pr2}$

我发现的

每个attribute_handle_value_pair的value字段的大小是从其父字段的length派生出来的。我认为sizeof()方法试图先计算attribute_handle_value_pair的大小,而read_by_type_response的length字段仍未定义,因此无法计算其大小。在

我尝试将value字段的长度改为静态值,效果很好。在

我的问题

如何计算依赖于其父构造的构造的sizeof()?在

我应该重新设计这个协议的建模方式吗?如果是,那怎么办?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值