Created
November 23, 2024 16:47
-
-
Save commandblockguy/d982743ea8b059a591915c65893ee62b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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