我有这个:
from typing import Tuple
import random
a = random.randint(100) # some random number
def foo(a: int) -> Tuple:
b = []
for _ in random.randint(0, 10):
b.append(random.randint(-5, 5) # adding random numbers to b
return a, *b
我想为这个函数写返回类型,但我现在不知道如何正确地做到这一点:
我试过这个:
from typing import Tuple
import random
a = random.randint(100) # some random number. It doesn't matter
def foo(a: int) -> Tuple[int, *Tuple[int, ...]]:
b = []
for _ in random.randint(0, 10):
b.append(random.randint(-5, 5) # adding random numbers to b
return a, *b
Pycharm with mypy说:foo(a: int) -> Tuple[int, Any] 但我需要函数返回传递给它的变量类型
在实际项目中,它采用泛型并返回一个元组,其中包含对象和解包列表以提高可读性。
实函数:
...
def get_entities_with(self, *component_types):
for entity in self.entities.values():
require_components = [component for component in entity.components if type(component) in component_types]
if len(require_components) == len(component_types):
yield entity, *require_components
...
.py 文件:
T = TypeVar("T")
...
def get_entities_with(self, *components:Type[T]) -> Generator[Entity, *Tuple[T, ...]]: ...
LEATH
相关分类