1

I am developing a descriptor basing myself in the ideas of @property. What I would like is to understand if there is any way of easily extending the behavior of property beyond setting/getting.

The concrete example: I am communicating with a device, and would like to have a property that can set/get/update. In this way, when I set, I pass the new value to the device, and store it in the cache. When I get, I retrieve the cached value. When I update, the device is re-asked for the value. In this way, can avoid unnecessary communication with the device, unless explicitly triggered.

I don't know if there is a pattern that I am missing that may be a straightforward solution. An option would be to invalidate the cache when using a special value in set, but I don't think it is a good idea to assume a specific value for triggering the update.

7
  • I mean, you'r get function would essentially contain that sort of logic. Here's an example: github.com/pyglet/pyglet/blob/… which rely quite hevily on this. Your set could check if the value/key already exists and take action accordingly. Commented May 8, 2020 at 14:28
  • Does not look like what I am looking for. I don't see how they implement the update while getting, they are updating while setting. I'll extend my question. Commented May 8, 2020 at 14:33
  • 2
    Even if there is a way to extend the property function like you described, what would the syntax look like to make use of that additional functionality? Assume a is a property of class A, then b = A.a calls a.getter , while A.a = b calls a.setter. What syntax would you suggest that calls the 'updater'? Commented May 8, 2020 at 14:37
  • That is what I am asking, is there a way? For example, del triggers a special method. Perhaps there is an upd. Or perhaps descriptors are not the way to go and there is another approach I am not aware of. Or perhaps there is a way of knowing whether get is being accessed with an extra .update? Commented May 8, 2020 at 14:48
  • 2
    I'm sorry, I don't understand the difference between your getter and updater. If you want your updater to have its own syntax, you would need to define a new operator. Commented May 8, 2020 at 14:56

1 Answer 1

1

I still don't understand the difference between your getter and updater, but here is some sample code which implements a cache that stores previous variable values. Maybe this helps you to find what you are looking for.

class A:
    def __init__(self):
        self._a_cache = [1]

    @property  # <- this defines a.getter
    def a(self):
        return self._a_cache[-1]

    @a.setter
    def a(self, value):
        self._a_cache.append(value)

    def undo(self):
        if len(self._a_cache) > 1:
            self._a_cache.pop()
        else:
            print('No previous values')

    def update(self):
        # what is supposed to happen here as opposed to the getter?
        pass
        
        
var = A()
print(var.a)

var.a = 10
print(var.a)

var.undo()
print(var.a)

var.update()
# ?
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.