Wie kann ich Python sagen, dass der Ergebnistyp einer Pipeline ein bestimmter Typ ist?Python

Python-Programme
Guest
 Wie kann ich Python sagen, dass der Ergebnistyp einer Pipeline ein bestimmter Typ ist?

Post by Guest »

Ich habe eine Pipeline -Klasse, die auf der Reduzierung der Funktionsfunktion basiert. Ich habe heute den Code berücksichtigt, um Informationen verfolgen zu können, und stellte fest, dass die Reduzierung der Funktionsinformationen die Typinformationen beseitigt, sodass ich den Typ nicht verfolgen kann. Siehe unten Code: < /p>
from functools import reduce
from typing import Callable, TypeVar, TypeAlias

def int_to_str(i: int) -> str:
return str(i)

def str_to_float(s: str) -> float:
return float(s)

pipes = [int_to_str, str_to_float]

_S = TypeVar("S")
_T = TypeVar("T")
Pipe : TypeAlias = Callable[[_S], _T]

def reducer(prev: _S, pipe: Pipe[_S, _T]) -> _T:
return pipe(prev)

initial_arg = 100
# Pylance shows that the type of what_type is int,
# because reduce return type annotation(_T@reduce) is same as the type of initial_arg.
what_type = reduce(reducer, pipes, initial_arg)
# what_type=100.0 (float value)
print(f"{what_type=}")
# type(what_type)= (float type)
print(f"{type(what_type)=}")
< /code>
Die Elemente in der Liste als Rohre sind wörtlich. Ich dachte also, es könnte statisch nachvollziehbar sein. < /p>

Python 3.10 < /li>
< /ul>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post