getattr with custom string class

92 views Asked by At

I have a custom class for wrapping immutable object to mutable objects.

It works fine for most operations, e.g., "+=", f-string, etc.

However, when I try to call getattr(obj, Variable("attr")), it always raises TypeError: getattr(): attribute name must be string. I have hacked __class__ method to make it believes is instance(Variable("attr"), str) = True. How can I solve this issue?

MWE:

class Variable:
    def __init__(self, value) -> None:
        self.storage = [value]

    @property  # type: ignore
    def __class__(self) -> type:
        return self.value.__class__

    @property
    def value(self) -> Any:
        return self.storage[0]

    @value.setter
    def value(self, value) -> None:
        self.storage[0] = self._get_value(value)

    def get(self) -> Any:
        return self.value

    def set(self, value) -> None:
        self.value = value
0

There are 0 answers