See More

#!/usr/bin/env PYTHONHASHSEED=1234 python3 # Copyright 2014-2019 Brett Slatkin, Pearson Education Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Example 10 # Check types in this file with: python -m mypy from typing import Callable, List, TypeVar Value = TypeVar('Value') Func = Callable[[Value, Value], Value] def combine(func: Func[Value], values: List[Value]) -> Value: assert len(values) > 0 result = values[0] for next_value in values[1:]: result = func(result, next_value) return result Real = TypeVar('Real', int, float) def add(x: Real, y: Real) -> Real: return x + y inputs = [1, 2, 3, 4j] # Oops: included a complex number result = combine(add, inputs) assert result == 10