Sunday, 18 June 2023

How do I get the generic class property in python

I have a class like:

class ExampleClass(BaseClass[Someclass]):
  pass


class BaseClass(AbstractBaseClass, Generic[T]):
  pass

I want to be able to do something like ExampleClass.targetType where I will return Someclass.__name__ how can I do this inside BaseClass I cannot seem to do T.__name__

I can workaround by defining a method like

class ExampleClass(BaseClass[Something]):
    version = 1

    def get_target_class_name() -> str:
        return Something.__name__

But I will need to copy this for every class



from How do I get the generic class property in python

No comments:

Post a Comment