# Python question: "observer pattern" actions for dictionary?

**URL:** https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149
**Category:** Programming questions
**Created:** [25 January 2021 13:37 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149 "2021-01-25T13:37:07Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gsteele13](https://forum.kavli.tudelft.nl/letter_avatar_proxy/v4/letter/g/3d9bf3/32.png) [@gsteele13](https://forum.kavli.tudelft.nl/u/gsteele13)
#### Post date: [25 January 2021 13:37 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/1 "2021-01-25T13:37:07Z")

</div>

Hi all,

I’m learning to write classes in python and am trying to understand better default actions:

> <https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change>

In particular, I’m working on a class for set of instruments. It turns out that this class of instruments has a help message that documents the full command interface, and the structure of this help is the same for all instruments from that manufacturer (“Windfreak Tech”, cool USB microwave generators and up/down converters).

My idea is that rather than hardcoding the parameters of the class, I can parse them from the help message into a dictionary. So my class has basically just a dictionary with entries like:

```auto
wft.value["Frequency (MHz)"]

```

I would like be able to type something like this:

```auto
wft.value["Frequency (MHz)"] = 1000.3

```

and then have this automatically run the `write()` code to set this value (and query it back maybe).

As I’m new to using observer properties in python, I was wondering: what is the best way to do this? I would somehow need to pass both the key and the value to my `setter` function?

Thanks in advance!  
Gary

---

<div class="post-metadata">

### Author: ![gsteele13](https://forum.kavli.tudelft.nl/letter_avatar_proxy/v4/letter/g/3d9bf3/32.png) [@gsteele13](https://forum.kavli.tudelft.nl/u/gsteele13)
#### Post date: [25 January 2021 13:41 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/2 "2021-01-25T13:41:37Z")

</div>

In case you’re interested, you can see the code here:

> <https://github.com/gsteele13/wft-generic/blob/main/wft.py>

---

<div class="post-metadata">

### Author: ![anton-akhmerov](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/anton-akhmerov/32/4_2.png) [@anton-akhmerov](https://forum.kavli.tudelft.nl/u/anton-akhmerov)
#### Post date: [25 January 2021 14:14 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/3 "2021-01-25T14:14:41Z")

</div>

Instead of storing this information in a barebones dictionary, you can implement your custom container that executes the necessary code when modified. An observer pattern is more complex.

Here’s how it could work.

```python
class VerboseDict(dict):
    def __setitem__ (self, key, value):
        print(f"Setting {key} to {value}")
        # Call the dictionary method using the super built-in
        super(). __setitem__ (key, value)

    def __getitem__ (self, key):
        print(f"You have requested {key}, here you go.")
        return super(). __getitem__ (key)

data = VerboseDict()
data["Frequency"] = 100
print(data['Frequency'])

```

prints

```auto
Setting Frequency to 100
You have requested Frequency, here you go.
100

```

---

<div class="post-metadata">

### Author: ![gsteele13](https://forum.kavli.tudelft.nl/letter_avatar_proxy/v4/letter/g/3d9bf3/32.png) [@gsteele13](https://forum.kavli.tudelft.nl/u/gsteele13)
#### Post date: [25 January 2021 14:16 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/4 "2021-01-25T14:16:12Z")

</div>

yeah, that’s a good solution!

(as I was writing my post I was already thinking something like that might be best 🙂…sometimes just formulating the question you come up with the answer…)

---

<div class="post-metadata">

### Author: ![gsteele13](https://forum.kavli.tudelft.nl/letter_avatar_proxy/v4/letter/g/3d9bf3/32.png) [@gsteele13](https://forum.kavli.tudelft.nl/u/gsteele13)
#### Post date: [25 January 2021 14:20 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/5 "2021-01-25T14:20:15Z")

</div>

Do I understand correctly that:

```auto
class VerboseDict(dict):

```

is the way to specify that Verbose dict is a child (subclass? not sure I know the right words…) of dict, and inherits all of it’s methods / data?

and then `super()` is the way you access the data / methods of the parent class?

(and I guess I cannot write `super[key] = value`, but instead need to use the “internal” methods ` __settitem__ `?)

Thanks from a python-object-oriented-programming-noob…

---

<div class="post-metadata">

### Author: ![anton-akhmerov](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/anton-akhmerov/32/4_2.png) [@anton-akhmerov](https://forum.kavli.tudelft.nl/u/anton-akhmerov)
#### Post date: [25 January 2021 14:41 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/6 "2021-01-25T14:41:24Z")

</div>

> [@gsteele13](#):
>
> Do I understand correctly that:
> 
> ```auto
> class VerboseDict(dict):
> 
> ```
> 
> is the way to specify that Verbose dict is a child (subclass? not sure I know the right words…) of dict, and inherits all of it’s methods / data?

Indeed. `VerboseDict` is a child of `dict`.

> [@gsteele13](#):
>
> (and I guess I cannot write `super[key] = value`, but instead need to use the “internal” methods ` __settitem__ `?)

`super()` is a weird object, so I’m not extremely certain why you can’t do `super()[key] = value`, but it seemed to not work. 🤷‍♂️

---

<div class="post-metadata">

### Author: ![slavoutich](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/slavoutich/32/9_2.png) [@slavoutich](https://forum.kavli.tudelft.nl/u/slavoutich)
#### Post date: [13 February 2021 09:03 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/7 "2021-02-13T09:03:08Z")

</div>

May be I am late to the party, but I would use Traitlets for implementing data structures with observer  
[https://traitlets.readthedocs.io/en/stable/api.html#callbacks-when-trait-attributes-change](https://traitlets.readthedocs.io/en/stable/api.html#callbacks-when-trait-attributes-change)

---

<div class="post-metadata">

### Author: ![gsteele13](https://forum.kavli.tudelft.nl/letter_avatar_proxy/v4/letter/g/3d9bf3/32.png) [@gsteele13](https://forum.kavli.tudelft.nl/u/gsteele13)
#### Post date: [19 February 2021 21:21 UTC](https://forum.kavli.tudelft.nl/t/python-question-observer-pattern-actions-for-dictionary/149/8 "2021-02-19T21:21:32Z")

</div>

Thanks Slava! I keep running into the word “traitlets” and I had no idea what they were 🙂 They sound awesome, I’ll have to do some reading.

Cheers,  
Gary
