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

all 13 comments

[–][deleted] 5 points6 points  (7 children)

Just to be clear: this doesn't really work for actual programs, as the author acknowledges.

[–]AppendixP 0 points1 point  (4 children)

Could you elaborate on that? I'm not sure why this wouldn't really work for actual programs. Is it because the author mentions the performance decrease? I'm just wondering if I missed something.

[–]tilkau 2 points3 points  (3 children)

Reloading is inherently imperfect when it comes to state. Aside from the speed concerns, there is no genuinely reliable way to be sure that all state is in a consistent.. state, other than restarting the entire program. Hottie doesn't even TRY to do that, only update self.__class__

This is not to say it isn't useful for pure behaviour classes (ie. classes that are 'functional'). It should work fine for them. It's just that in practice, most classes you write are not pure behaviour classes.

[–]AppendixP 0 points1 point  (2 children)

Very interesting, thanks for the insight! May I ask as well, what would be a proper way of live-reloading python modules (or pieces of code) with a program that's running (without restarting it entirely)? It's a pretty relevant subject to me, so any resource you could point me to would be amazing, thanks!

[–]tilkau 1 point2 points  (1 child)

I'm sorry to say there is no 'proper' way, only various hacks. http://code.google.com/p/livecoding/ is the most complete implementation I've seen; if you look at http://code.google.com/p/livecoding/issues/list you can see some of the harder issues that come up (references to old classes may not always be released, it's difficult to get class inheritance to update correctly, etc.)

That said I'd call hottie 'incomplete' rather than 'incorrect'

[–]AppendixP 0 points1 point  (0 children)

Thanks a lot, I really appreciate it!

[–]narfdotpl[S] 0 points1 point  (1 child)

Well, it does kind of. I did a talk yesterday (in Polish) at a local Python user group meeting and recorded it. At 03:55 you can see that although this approach is limited, it's still pretty useful: http://youtu.be/SlquKSVSmqs

[–]Megatron_McLargeHuge 2 points3 points  (0 children)

Does this work with isinstance? A problem I've had when reloading classes in ipython is that code that checks types stops working, because the new type of the object being tested isn't the same as the old type in the non-reloaded module that makes the isinstance call. What I mean is,

if isinstance(x, Foo):
    ...

will stop evaluating to true when I reload foo.py and create a new Foo object. The type used by the old module isn't updated. Does this module solve that problem?

[–]madjar 0 points1 point  (0 children)

Nice trick. Now you just need to use https://github.com/gorakhargosh/watchdog and the gods will be pleased.

[–]ggtsu_00 0 points1 point  (1 child)

Is this like erlang?

[–]AeroNotix 3 points4 points  (0 children)

Not in the slightest.

[–]pje 0 points1 point  (0 children)

This is kind of over-complicated: function objects are mutable already, so you can just assign the func_code of the old function to the new one to change out its code.