The @property.getter and @property.setter decorators are
clever, but they have the disadvantage that you end up
writing the name of the property no less than 5 times,
all of which have to match.

Thinking there must be a better way, I came up with this:

def Property(name, bases, dict):
    return property(dict.get('get'), dict.get('set'))

which allows you to write

class Test:

    class foo(metaclass = Property):

        def get(self):
            print("Getting foo")
            return self._foo

        def set(self, x):
            print("Setting foo to", x)
            self._foo = x

test = Test()
test.foo = 42
print(test.foo)

Output:
Setting foo to 42
Getting foo
42

--
Greg
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LYNBL6D2HFYHQGQCBXFKJO6W3JRZYJZ5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to