ipython / ipython/traitlets

Support observe as a decorator outside of classes

Đang mở
#273 7 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
653
Fork
217
Merge trung bình
2 ngày 21 giờ
Pull request đã merge (30 ngày)
2

Mô tả

Please note that I have only used `traitlets` as a part of `ipywidgets`, and not sure how feasible are the suggestions below for its other uses - but for widgets they would be quite useful.

So, when one needs to `observe` a widget trait, it can't be done without separately defining the handler function and then subscribing:

``` python
def handler(*_):
# ... code ...

w.observe(handler, 'value')
```

Wouldn't it be better if you could just write like this:

``` python
@w.observe('value')
def _(*_):
# ... code ...
```

This way there is also no need to make a name for the handle function.

However, it's often useful to call the handler once after setting everything up, e.g. if you make some plots in the handler it makes sense to display them without forcing user to move a slider or something. So, I also suggest adding an argument to `observe`, like `run=True` (looking for a better name :) ) or a separate method `observe_and_run` which would also execute the handler (in addition to subscribing). This way we still wouldn't have to think of a name for the handler function.

So, instead of:

``` python
def handler(*_):
# ... code ...

w.observe(handler, 'value')
handler()
```

we would be able to write:

``` python
@w.observe('value', run=True)
def _(*_):
# ... code ...
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.