类继承的递归错误 [英] Recursion error with class inheritance
问题描述
我有一个文件,其中包含要用于存储API端点的类。
我想使用类的原因是,这样我可以通过键入 api.level2.resources
来访问端点。
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
.
这是文件的外观,其中以 API
为主要类,而 SubEntries
为 child':
Here is what the file looks like, with API
as the main class and SubEntries
the 'child':
class API(object):
"""
A class for logging to stdout and/or a file. Supports color output for different log kinds.
"""
def __init__(self):
"""
:param log_to_file: Bool - Whether to log to a file or only to stdout (False)
:param s: String - Log file name without extension for success logs
:param e: String - Log file name without extension for error logs
:param prefix: Bool - Whether to show the prefix or not
:param timestamp: Bool - Whether to show the timestamp or not
:param debug: Bool - Whether to show debug messages or not
"""
self.login = '/login'
self.logout = '/logout'
self.sysRequest = '/sysReq'
self.level2 = SubEntries()
class SubEntries(API):
def __init__(self):
super().__init__()
self.host_info = '/info'
self.resources = '/resources'
但是,当我尝试像这样使用它时:
But, when I try and use it like this:
from src import API
api = API()
print(api.level2.resources)
我收到以下错误:
Traceback (most recent call last):
File "D:/_projects/pynap/new.py", line 4, in <module>
api = API()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
...
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
File "D:\_projects\pynap\src\QAPI.py", line 24, in __init__
super().__init__()
File "D:\_projects\pynap\src\QAPI.py", line 18, in __init__
self.level2 = SubEntries()
RecursionError: maximum recursion depth exceeded while calling a Python object
我很确定解决方案很简单,但我不确定如何将类构造为
I am pretty sure the solution is simple, I am just not sure how to structure the class to be able to use it like I want.
推荐答案
正如我在评论中所说,您正在明确创建一个循环引用,因此在某一点上它达到了Python的递归限制。有很多方法可以避免相似类型对象的递归。最简单的是拥有一个共同的父母,例如:
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:
class BaseAPI(object):
# place here whatever you want common for all API/SubEntry objects
pass
class API(BaseAPI):
def __init__(self):
self.login = '/login'
self.logout = '/logout'
self.sysRequest = '/sysReq'
self.level2 = SubEntries()
class SubEntries(BaseAPI):
def __init__(self):
super(BaseAPI, self).__init__()
self.host_info = '/info'
self.resources = '/resources'
您也可以覆盖 __ getattr __()/ __ setattr __( )/ __ delattr __()
您的 BaseAPI
类中的方法,然后动态评估每个属性访问。您还可以将端点 dict
传递给您的 BaseAPI
类,并使其更新其 self .__ dict __
从传递的 dict
...
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.
这篇关于类继承的递归错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!