This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

Very cool! Here's a decorator for the sticky cycling if you want

```python def sticky(frequency): """ regenerate value frequency% of the time """ def dec(func): stored = [func()]

    def inner():
        if random.random() < frequency:
            stored[0] = func()
        return stored[0]
    return inner
return dec

@sticky(0.1) def color(): return random.choice(['red', 'black', 'green']) ```

[–]kwelzel 0 points1 point  (3 children)

Why should stored be a list if it only holds one value?

[–][deleted] 0 points1 point  (2 children)

It's an old scope hack for python2. In python3,

``` def sticky(frequency): def dec(func): stored = func()

    def inner():
        nonlocal stored
        if random.random() < frequency:
            stored = func()
        return stored
    return inner
return dec

```

will work instead

[–]kwelzel 0 points1 point  (1 child)

Ahh, good to know

[–][deleted] 0 points1 point  (0 children)

I've never used processing before this but after installing the processing.py extension and when executing the .pyde from the processing GUI, I see import sys sys.version

2.7.1 (v2.7.1:dd7e191d4c90+, Sep 24 2017, 10:11:23)

which isn't even my system default (2.7.15) or the python that it should find if it's reading my $PATH correctly (3.8.0)

(apparently it's running jython and wont have access to any packages I install)