4 ms·
importlib.reload does not "just work". https://docs.python.org/3/library/importlib.html#importlib.reload https://docs.python.org/3/library/importlib.html#import
by oefrha 6y ago
importlib.reload does not "just work". https://docs.python.org/3/library/importlib.html#importlib.reload https://docs.python.org/3/library/importlib.html#importlib.r... documents what is done and not done.
> Doesn't "isinstance" break if you redefine the class?
With importlib.reload, yes.
You might be interested in IPython's autoreload implementation, which does not break isinstance: https://github.com/ipython/ipython/blob/f8c9ea7db42d9830f16318a4ceca0ac1c3688697/IPython/extensions/autoreload.py#L253-L417 https://github.com/ipython/ipython/blob/f8c9ea7db42d9830f163... However, you could still end up with semi-broken instances, for instance if you update
class A:
pass
to
class A:
def __init__(self):
self.attr = "something"
def __str__(self):
return self.attr
> By the way, you may want to use `import traceback; traceback.print_exc()` to log exceptions. I'm not sure how that plays with python's `logger` class though.
PSL logging has builtin integration with exception tracebacks. See Logger.exception [1] or the exc_info keyword param to Logger.debug/info/warning/error/critical/log [2].
[1] https://docs.python.org/3/library/logging.html#logging.Logger.exception https://docs.python.org/3/library/logging.html#logging.Logge...
[2] https://docs.python.org/3/library/logging.html#logging.Logger.debug https://docs.python.org/3/library/logging.html#logging.Logge...
- aidos 6y agoI couldn’t live without ipython autoreload. You need to know where the limitations are that require you to create your instances again. Things like sqlalchemy model changes run into issues because of the meta class magic (I think, not looked too deeply). I feel like this library is a bit misrenamed. It seems to be more like “autorun”. Edit: here’s the ipython config I use to turn it on https://github.com/aidos/dotfiles/blob/master/_config/ipython/profile_default/ipython_config.py https://github.com/aidos/dotfiles/blob/master/_config/ipytho...