Python: Get source code of class (using inspect)

Viewed 7154

I am using ipython and I want to save a class defined in one of my notebooks. I can do this with functions nicely with %save and inspect.getsource, but I cant seem to get the source of my class. I had a quick look at the methods in inspect and couldnt seem to find anything that could help. Any suggestions?

class A():
    def __init__(self):
        self.x = 1

%save filename.py inspect.getsource(A)

inspect.getsource(A)
>>> ...
>>> TypeError: <module '__main__'> is a built-in class
1 Answers
Related