Ok... if you can save your def as a script you can import the function from within the script and reuse the code block... I suspect this isn't the easy solution that you want short of getting into importing modules, namespace and the like. but here goes.
>>> from angles_demo import _demo
>>> import inspect
>>>
>>> codeblock = inspect.getsource(_demo)
>>> print(codeblock)
def _demo():
"""A sample run of compass returning compass notations for 20 deg
increments. Change to suit
"""
angles = np.arange(0, 360, 20)
for i in angles:
print("{} => {}".format(i, compass(i)))
rose = c[np.digitize(angles, a)]
comp_rose = list(zip(angles,rose))
print("\nCompass rose examples\n{}".format(comp_rose))
return comp_rose
and codeblock is a string. For you, the easiest thing would just to keep a script of your code blocks in the same folder as the script you are using... import the code block from the script and get the script. Or better still, you could just copy and paste the 'stuff' into the code you are working on... whatever is easiest is the best... but I suspect you aren't creating packages as of yet that you need to maintain.