J. Siek
J. Siek

Reputation: 43

Repeated fields return AttributeError - type object 'Foo' has no attribute 'nums'

I am following this tutorial for using protocol buffers in python.

Here is my main.proto file:

syntax = "proto2"
message Foo {
    repeated int32 nums = 1;
}

I cannot access foo.nums.

Here is the python script which fails:

import main_pb2
foo = main_pb2.Foo
foo.nums.append(1)

This is what I get when I run it:

AttributeError: type object 'Foo' has no attribute 'nums'.

To make it even more confusing, I can see that Foo has an attribute called foo.NUMS_FIELD_NUMBER. What is going on?

Upvotes: 0

Views: 346

Answers (1)

J. Siek
J. Siek

Reputation: 43

This line:

foo = main_pb2.Foo

should have been:

foo = main_pb2.Foo()

I wasn't calling the constructor. That fixes the error.

Upvotes: 1

Related Questions