Skip to content Skip to sidebar Skip to footer

How Do I Return The Definition Of A Class In Python?

Say I have a class 'NumberStore' class NumberStore(object): def __init__(self, num): self.num = num def get(self): return self.num And later on, for the p

Solution 1:

Yep, with inspect.getsource:

from inspect import getsource

classNumberStore(object):
    def__init__(self, num):
        self.num = num

    defget(self):
        return self.num

    @classmethoddefprint_class_definition(cls):
        return getsource(cls)

Solution 2:

Use inspect.getsource.

importinspectsource_text= inspect.getsource(NumberStore)

Post a Comment for "How Do I Return The Definition Of A Class In Python?"