名称未在类型注释

时间:2016-03-29 14:13:18

标签: python python-3.x type-hinting

我目前正在创建一个python线性代数模块,用于娱乐和练习语言。我最近尝试向模块添加类型注释,如下:

class Vector:
     # Various irrelevant implementation details
     def __add__(self, other: Vector) -> Vector:
        # More implementation details....

但是,当我尝试导入此内容时,它会吐出NameError: Name 'Vector' is not defined。我承认这个问题已经以here的形式得到了回答,但它似乎并没有完全为我的情况提供答案。

我想知道的事情:

  • 我已在此文件中按字面定义了该类。为什么说这个名字没有定义?
  • 如何以可以用于注释的方式定义Vector(作为type)?

2 个答案:

答案 0 :(得分:39)

你有前瞻性声明;函数(作为方法绑定)是在之前创建的,因此名称Vector尚不存在。只有当所有类主体都已执行时,Python才能创建class对象并将名称Vector绑定到它。

只需使用带有名称的字符串:

class Vector:
     # Various irrelevant implementation details
     def __add__(self, other: 'Vector') -> 'Vector':
        # More implementation details....

这不会影响IDE查看声明的方式;加载整个模块后,将查找字符串,并在当前上下文中将其解析为有效的Python表达式。由于在加载整个模块后类Vector存在,因此字符串'Vector'可以正确转换为类对象。

另见specification on forward references

  

当类型提示包含尚未定义的名称时,该定义可以表示为字符串文字,稍后要解决。

     

[...]

     

字符串文字应该包含一个有效的Python表达式[...],一旦模块完全加载,它应该在没有错误的情况下进行评估。

答案 1 :(得分:15)

如果您使用的是 Python 3.7 及以上版本。看看Postponed evaluation of annotations

从Python 3.7开始,它将被允许,只需添加:

#include <iostream>
#include <string>

struct CityData {
  std::string name;
  double population;
  short id;
};

void printCityData(CityData city) {
  std::cout << "City Name : " << city.name << std::endl;
  std::cout << "Population : " << city.population << " Milion" << std::endl;
  std::cout << "City ID : " << city.id << std::endl;
}

int main() {
  std::string NOP = "CityData AI"; // NOP stands for NAME OF PROGRAM
  std::cout << NOP << std::endl;

  // Create space between Name of program and user input
  std::cout << '\n';

  // CITYDATA FULL INFO
  CityData Toronto = {"Toronto", 2.1, 1};
  CityData Montreal = {"Montreal", 1.7, 2};
  CityData Ottawa = {"Ottawa", 1, 3};
  // Declare choice outside of DO-WHILE statement
  std::string choice;
  do {
    std::cout << "Enter city name or see the city list : " << std::endl;
    std::string cnl; // CN stands for City Name
    getline(std::cin, cnl);

    // Create space between user input and city info print
    std::cout << '\n';

    // City name choice - start of
    if (cnl == "Toronto" || cnl == "toronto" || cnl == "to") {
      printCityData(Toronto);
    } else if (cnl == "Montreal" || cnl == "montreal" || cnl == "mo") {
      printCityData(Montreal);
    } else if (cnl == "Ottawa" || cnl == "ottawa" || cnl == "ot") {
      printCityData(Ottawa);
    } else if (cnl == "city list" || cnl == "City List" || cnl == "City list") {
      std::cout << "Currently on list : Toronto, Montreal, Ottawa" << '\n';
    } else {
      std::cout << "City is not on the list!" << std::endl;
    }
    // City name If statement outro

    // Create space between data print and end of program
    std::cout << '\n';

    std::cout << "Any additional info needed?" << '\n';
    getline(std::cin, choice);
  } while (choice ==
           "Yes"); // This acts as the second if statement in original code

  system("pause");
  return 0;
}

还要注意

  

它将成为Python 4.0的默认设置。