重复的字段返回AttributeError-类型对象'Foo'没有属性'nums'

时间:2018-08-17 21:04:18

标签: python python-3.x protocol-buffers

我正在关注this tutorial,以在python中使用协议缓冲区。

这是我的main.proto文件:

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

我无法访问foo.nums

这是python脚本,它失败了:

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

这是我运行它时得到的:

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

为了使它更加混乱,我可以看到Foo有一个名为foo.NUMS_FIELD_NUMBER的属性。发生了什么事?

1 个答案:

答案 0 :(得分:1)

此行:

static public int calc(int[][] grid, int rows, int columns){

int[][] check = new int[rows][columns];

int max, pos;

for(int i=0; i<rows; i++){
    max = grid[i][0];
    pos = 0;
    for(int j=0; j<columns; j++){
        if(grid[i][j] >= max){
            max = grid[i][j];
            pos = j;
        }
    }
    check[i][pos] = max; // updated here
}

for(int j=0; j<columns; j++){
    max = grid[0][j];
    pos = 0;
    for(int i=0; i<rows; i++){
        if(grid[i][j] >= max){
            max = grid[i][j];
            pos = i;
        }
    }
    check[pos][j] = max; // updated here
}

// UPDATE HERE
// these loops look for rows that have two 
// max values (one for a column and one for a row) that are equal
for(int j=0; j<rows; j++){
  max = 0;
  for(int i=0; i<columns; i++){
    if(check[j][i] != 0) {
      if (max == 0){
        max = check[j][i];
      }else if (max == check[j][i]){
        // reset
        check[j][i] = 0;
      }
    }
  }
}

int total = 0;
for(int i=0; i<rows; i++){
    for(int j=0; j<columns; j++){
        if(check[i][j]==0){ // updated here
            total += grid[i][j];
        }
    }
}
return total;
}

应该是:

foo = main_pb2.Foo

我没有打电话给构造函数。这样可以修复错误。