AttributeError:不允许对复合字段“任务"进行分配在协议消息对象中 [英] AttributeError: Assignment not allowed to composite field "task" in protocol message object

查看:76
本文介绍了AttributeError:不允许对复合字段“任务"进行分配在协议消息对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用protocol-buffers python lib发送数据,但是有一些问题,所以

I'm using protocol-buffers python lib to send data,but it's have some problems, so

Traceback (most recent call last):
  File "test_message.py", line 17, in <module>
    ptask.task = task
  File "build\bdist.win32\egg\google\protobuf\internal\python_message.py", line
513, in setter
AttributeError: Assignment not allowed to composite field "_task" in protocol message object.

src 如下:

原型文件:

message task {
    required int32 id = 1;
    required string msg = 2;
}

message task_info {
    required task task = 1;
}

python 代码:

task = yacc.task()
task.id = 1000
task.msg = u"test"
ptask = yacc.task_info() 
ptask.task = task # this line happen the runtime error 

推荐答案

我不知道 protocol-buffers 但我看了一下 the docs 它说:

I don't know protocol-buffers but I took a look at the docs and it says:

您不能为嵌入的消息字段赋值.反而,为子消息中的任何字段赋值意味着在父级中设置消息字段.

You cannot assign a value to an embedded message field. Instead, assigning a value to any field within the child message implies setting the message field in the parent.

所以我假设这应该可行:

So I'm assuming this should work:

task = yacc.task()
task.id = 1000
task.msg = u"test"
ptask = yacc.task_info() 
ptask.task.id = task.id
ptask.task.msg = task.msg

这篇关于AttributeError:不允许对复合字段“任务"进行分配在协议消息对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆