Skip to content Skip to sidebar Skip to footer

Recursion Error With Class Inheritance

I have a file containing classes which I want to use to store API endpoints. The reason I want to use classes, is so that I can access the endpoints by typing api.level2.resources.

Solution 1:

As I've said in my comment, you're quite explicitly creating a circular reference here so at one point it hits Python's recursion limit. There are a lot of ways to avoid recursion of similar-typed objects. The simplest is to have a common parent, for example:

classBaseAPI(object):
    # place here whatever you want common for all API/SubEntry objectspassclassAPI(BaseAPI):

    def__init__(self):
        self.login = '/login'
        self.logout = '/logout'
        self.sysRequest = '/sysReq'
        self.level2 = SubEntries()

classSubEntries(BaseAPI):

    def__init__(self):
        super(BaseAPI, self).__init__()
        self.host_info = '/info'
        self.resources = '/resources'

You can also override __getattr__()/__setattr__()/__delattr__() methods in your BaseAPI class and then have every property access dynamically evaluated. You can also pass an 'endpoints' dict to your BaseAPI class and have it update its self.__dict__ to get endpoints from a passed dict...

Your question lacks specificity to suggest what would be the best approach.

Post a Comment for "Recursion Error With Class Inheritance"