Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
Last active December 21, 2015 03:29
Show Gist options
  • Select an option

  • Save ericmoritz/6242558 to your computer and use it in GitHub Desktop.

Select an option

Save ericmoritz/6242558 to your computer and use it in GitHub Desktop.

An example of hotcode reload in Python. I used this to reload a class on an instance in development without having to recreate the instance.

class Hello(object):
def __init__(self, name):
self.name = name
def greet(self):
print "Hello, " + self.name
import greeting
def rewrite():
"""Simulate editing a file"""
source = open("greeting.py", "rb").read()
source = source.replace('"Hello', '"Hola')
open("greeting.py", "wb").write(source)
x = greeting.Hello("Eric")
x.greet()
rewrite()
reload(greeting)
x.__class__ = greeting.Hello
x.greet()
#!/usr/bin/env bash
rm -f *.pyc
python main.py
git checkout greeting.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment