Skip to content

Instantly share code, notes, and snippets.

@commandblockguy
Created November 23, 2024 16:47
Show Gist options
  • Save commandblockguy/d982743ea8b059a591915c65893ee62b to your computer and use it in GitHub Desktop.
Save commandblockguy/d982743ea8b059a591915c65893ee62b to your computer and use it in GitHub Desktop.
import sys
from types import MethodType
class genericclassmethod:
def __init__(self, f):
self.f = f
def __get__(self, instance, owner=None):
caller = sys._getframe().f_back
if caller.f_code.co_qualname == '_BaseGenericAlias.__getattr__':
ga = caller.f_locals["self"]
return MethodType(self.f, ga)
raise TypeError(f"Call to {self.f.__name__} with no parameter specified")
class MyClass[T]:
@genericclassmethod
def print_type_param(cls):
print(f"T = {cls.__args__[0]}")
MyClass[int].print_type_param()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment