Question

I want to override the getter of a property in a class in python:

class Block(object) :

    def name():
        doc = "The name property."
        def fget(self):
            return self._name
        def fset(self, value):
            self._name = value
        def fdel(self):
            del self._name
        return locals()
    name = property(**name())

    def toString(self):
        string = "{0} {1} \n".format(self.__class__.__name__, self.name)
        return string

class Machine(Block) :

    def host():
        doc = "The host property."
        def fget(self):
            return self._host

        def fset(self, value):
            self._host = value

        def fdel(self):
            del self._host

        return locals()

    host = property(**host())

    name = property(**host())

I want the extended name property to return the host getter in Machine, so when i call toString(), the name becomes the host.

Was it helpful?

Solution

Do it the other way around:

name = property(**host())
host = property(**host())

or just:

host = property(**host())
name = host

The way you're doing it now, name = property(**host()) is working on the property host, which has substituted the method by then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top