autoreload and function decorator
See original GitHub issueI posted this on SO here: https://stackoverflow.com/q/49825052/5545005
I am not sure where I should have posted. Thinking here would have been better. Is there any advice on this issue?
Thanks.
My System: ‘commit_hash’: ‘ca5443062’, ‘commit_source’: ‘installation’, ‘default_encoding’: ‘utf-8’, ‘ipython_path’: ‘C:\Anaconda3\lib\site-packages\IPython’, ‘ipython_version’: ‘6.2.1’, ‘os_name’: ‘nt’, ‘platform’: ‘Windows-10-10.0.16299-SP0’, ‘sys_executable’: ‘C:\Anaconda3\python.exe’, ‘sys_platform’: ‘win32’, ‘sys_version’: '3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, ’ ‘03:27:45) [MSC v.1900 64 bit (AMD64)]’
I am fairly new to decorators but am experiencing unexpected behavior revolving around autoreload in an interactive workflow with decorated functions. Its best explained by example (note these are all cells in a jupyter notebook):
The decorator:
%%file testdec.py def decorated(func): print("decorating") def wrapped(*args, **kwargs): return func(*args, **kwargs) return wrapped
Where the decorator is used:
%%file testmod.py from testdec import decorated @decorated def thisfunc(): print("old output") def _thisfunc1(): print("old output 1") thisfunc1 = decorated(_thisfunc1)
I would use the following to call the decorated functions:
from testmod import * thisfunc() thisfunc1()
outputs:
decorating decorating old output old output 1
Now updating
testmod.py
with:%%file testmod.py from testdec import decorated @decorated def thisfunc(): print("new output") def _thisfunc1(): print("new output 1") thisfunc1 = decorated(_thisfunc1)
and calling the functions again:
thisfunc() thisfunc1()
gives the following, note the old output from the first method:
decorating decorating old output new output 1
However, explicitly reimporting from this module:
from testmod import * thisfunc() thisfunc1()
results in:
new output new output 1
Ideally the
@decorated
function (e.g. with the@
and not the second method) would autoreload transparently as the second method does. Is there something I can do to achieve this? What am I missing for decorated functions. For now we’re manually disabling decorators when editing interactively in order to have the benefits of autoreload.Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (1 by maintainers)
This bug is really annoying. None of the solutions mentioned so far in this post works, when it’s a decorated method rather than a decorated function. Perhaps I just rely too much on
autoreload
in my work flow. Hope this get’s fixed soon, or I am open to alternatives similar to this, by which I can use both jupyter and IDE at the same time.Just stumbled on this issue too, took me quite some time to figure out what was going wrong where. Hoping on a fix as well…