いろは3010814 views
高校日本史190571 views
数学講師2886227 views
LaTeX962153 views
中学英語811710 views
中学理科1630641 views
中学社会668760 views
雑学1473612 views
ヒストリア290598 views
英語613639 views
Help
Tools
NewsSpreadsheetCalendarBookkeepingMarkdown TablesSlidesTier ListPen ToolIllustrationCrayonWatercolorPixel ArtASCII ArtPerspectiveEndless StairsGraphMind MapER DiagramFamily TreeMemeCurved TextImage EditorMosaicRetro FilterPencil SketchSwirl EffectLine ArtOCR/HighlighterMakeup EditorFaviconVideo TrimmerScrolling VideoVideo TitleColor PickerColor ExtractorBonfireFireworksWater RippleWater SplashBreaking GlassWood GrainMarble TextureCSS ButtonIcon MakerBar ChartGrouped Bar ChartStacked Bar ChartPie ChartLine ChartArea ChartStacked Area ChartScatter Plot3D Bar Chart3D Pie ChartBar Chart RaceBubble ChartPopulation PyramidPictogramEarningsCandlestick ChartInvestment RiskMortgage SimulatorCalculatorMatrix CalculatorFunction GraphPolynomial ExpansionVenn DiagramField VisualizerRubik's Cube Group TheoryTraveling SalesmanVoronoi and DelaunayFractalColumn ArithmeticDraw Math FiguresArithmetic AnimationArithmetic Word ProblemsCounting with Tree DiagramsCube NetsRolling DiceCross SectionsMotion PathMechanicsWavesElectromagnetic WavesCapacitorsLight and LensesThermodynamicsHow Semiconductors WorkMolecular StructuresAtomic OrbitalsElectrochemical CellsChemical EquilibriumCrystal LatticesBuffer pHOrganic Reaction MapPeriodic TableComplex IonsDNA Double HelixCell DivisionMembrane ChannelsNerve ImpulseMuscle ContractionHormones and HomeostasisRock ClassificationWeatherConstellationsSolar and Lunar Eclipses3D ModelingFloor PlanSeismic StructuresIntersection TurnMaglevCooking AnimationOrigamiLive Viewer CountGeoJSON MapRailway MapPopulation MapCrime MapLand Price MapSchool MapShrine and Castle MapHouse of Representatives MapWord MapSolitaireReversiHakoiri MusumeChessHamburgerRippleSlide Puzzle MakerNeon PinballNovel MakerJapanese Typing PracticePiano Score EditorMusic TheoryShogi StrategyPiano Rhythm Game

English

Python の TypeVar とジェネリクス

TypeVar を使うと、ジェネリックな関数や型を定義できます。入力と出力の型の関係を保ちながら、さまざまな型に対応する関数を作れます。

基本的な使い方

TypeVar で型変数を定義し、関数の引数と戻り値で同じ型を表現します。

from typing import TypeVar

T = TypeVar("T")

def identity(x: T) -> T:
    return x

result1 = identity(42)       # int を受け取り int を返す
result2 = identity("hello")  # str を受け取り str を返す

型チェッカーは、引数の型から戻り値の型を推論します。

型の制約

bound を指定すると、その型またはサブクラスに制限できます。

from typing import TypeVar

class Animal:
    def speak(self) -> str:
        return "..."

class Dog(Animal):
    def speak(self) -> str:
        return "Woof!"

T = TypeVar("T", bound=Animal)

def make_speak(animal: T) -> T:
    print(animal.speak())
    return animal

特定の型のみ許可

複数の型を列挙して制限することもできます。

from typing import TypeVar

T = TypeVar("T", int, float)

def double(x: T) -> T:
    return x * 2  # type: ignore

double(5)    # OK
double(3.14) # OK
double("a")  # 型エラー

リストのジェネリクス

コンテナ型と組み合わせると、要素の型を保持できます。

from typing import TypeVar, List

T = TypeVar("T")

def first(items: List[T]) -> T:
    return items[0]

x = first([1, 2, 3])      # int
y = first(["a", "b"])     # str

TypeVar を活用すると、汎用的でありながら型安全なコードを書けます。