作为一名C++程序员,以下代码对我来说似乎很自然,但它无法运行:
作为 C++ 程序员,以下代码看起来非常自然,但它无法正常运行:
from typing import TypeVar, Generic, List, NewType
TPopMember = TypeVar('TPopMember')
Population = NewType('Population', List[TPopMember])
class EvolutionaryAlgorithm(Generic[TPopMember]):
def __init__(self, populationSize: int) -> None:
# The following raises TypeError: 'TypeVar' object is not callable
self.__population = Population([TPopMember() for _ in range(populationSize)])
显然,Python无法实例化实际上是TypeVars的类(TPopMember)。我只想创建一个列表(Population),其中包括一些默认初始化的(在Python中如何说?)TPopMembers。我该如何做?
我正在使用Python 3.7.2。
class
。 - Uli SotschokTPopMember
工厂给EvolutionaryAlgorithm
。 - Patrick Haugh