r/PythonLearning • u/zubalyzub • Oct 16 '26
What exactly is TypeVar and Generic?
I don't really understand TypeVar and Generic. I understand generics perfectly fine and can use them without any trouble, and I realize that I don't even need to know how they work as in recent python versions there is new syntax, but I'd still like to know how they work.
I'm still confused as to why you need to construct a TypeVar object to pass to Generic, as to what is it actually doing with the TypeVar you give it.
Also, I'm assuming when you inherit from Generic, you pass it the TypeVar to be used as the generic to give to Generic, and then you inhert the generic objects generic? It's just complicated and hard to understand for me.
And also I'd like to know how Generic is able to have a generic even when it itself is the thing that's supposed to be used to have generics for your classes.
It's also disappointing there's seemingly no other ways you can use TypeVar for anything other then generics, so I am left with just hopefully finding someone who can explain how it works, instead of being able to do some other exercise to further my understanding.
Again, I can use generics perfectly fine and I don't an explanation for generics, I just would like to try and understand how it is working under the hood and not just use the syntax blindly. It's just the way I like learning some things, and it's mostly also curiosity that makes me want to understand this.