on-internet
← Community

Difference between three forms of instantiating a generic class in Python

Stack Overflow ·

I have this generic class in Python 3.12: class Box[T]: def __init__(self, val: T): self.val = val I can instantiate it in 3 ways: box: Box[int] = Box(42) box = Box[int](42) box = Box(42) What's the difference? Do different type checkers (pyright, pyrefly, etc) interpret them differently? How about in legacy Python (<3.12), where the example would be: from typing import TypeVar, Generic T = TypeVar('T') class Box(Generic[T]): def __init__(self, val: T): self.val = val

Comments

Sign in or sign up to comment and upvote.

No comments yet.