2
topic.thriftファイルに共用体型を定義し、gen-pyを生成します。python thrift union型はシリアル化できませんか?
union Generic{
1: string s,
2: bool b,
3: i64 i,
4: double d}
struct Article{
1: string title,
2: string content,
3: Generic test}
と、このようなシリアル化コード:
transport_out = TTransport.TMemoryBuffer()
protocol_out = TBinaryProtocol.TBinaryProtocol(transport_out)
parse_item.write(protocol_out)
bytes = transport_out.getvalue()
parse_itemが条の目的である:
parse_item = Article()
parse_item.test = 1
どんなに列str、int型、ブール値、または二本のような parse_item.testに割り当てられた値、私はすべてこのようなエラーを受け取ります:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "gen-py/topic/ttypes.py", line 189, in write
self.test.write(oprot)
AttributeError: 'str' object has no attribute 'write'
私は本当に理由を知らないのですか?誰でもアイデアはありますか?
ええ、ああ:ここ
は動作するコードとの散歩です!ありがとう!私は、ユニオンの構造は難しいと思う!オプションの多分は代替選択肢を使用してください。 – Nan