python classmethod property 1

python classmethod property

class classproperty(property):
    def __get__(self, cls, owner):
        return classmethod(self.fget).__get__(None, owner)()

Here is what the above code is Doing:
1. classproperty is a subclass of property.
2. classproperty.__get__() is overridden to return the result of calling the classmethod() of the original property’s fget.
3. classmethod() is called with the original property’s fget and None as arguments.
4. classmethod() returns a classmethod object, which is then called with None and the owner class as arguments.
5. The classmethod object calls the original property’s fget with the owner class as the argument.
6. The result of calling the original property’s fget is returned.

Similar Posts