- class asyncpraw.models.comment_forest.CommentForest(submission: asyncpraw.models.Submission, comments: List[asyncpraw.models.Comment] | None = None)
A forest of comments starts with multiple top-level comments.
Each of these comments can be a tree of replies.
- await __call__()
Call self as a function.
- __getitem__(index: int)
Return the comment at position
indexin the list.
This method is to be used like an array access, such as:
first_comment = submission.comments
Alternatively, the presence of this method enables one to iterate over all top level comments, like so:
for comment in submission.comments: print(comment.body)
- __init__(submission: asyncpraw.models.Submission, comments: List[asyncpraw.models.Comment] | None = None)
submission – An instance of
Submissionthat is the parent of the comments.
comments – Initialize the Forest with a list of comments (default:
- list() List[asyncpraw.models.Comment | asyncpraw.models.MoreComments] | Coroutine[Any, Any, List[asyncpraw.models.Comment | asyncpraw.models.MoreComments]]
Return a flattened list of all Comments.
This list may contain
replace_more()was not called first.
- await replace_more(*, limit: int | None = 32, threshold: int = 0) List[asyncpraw.models.MoreComments]
Update the comment forest by resolving instances of
limit – The maximum number of
MoreCommentsinstances to replace. Each replacement requires 1 API request. Set to
Noneto have no limit, or to
0to remove all
MoreCommentsinstances without additional requests (default:
threshold – The minimum number of children comments a
MoreCommentsinstance must have in order to be replaced.
MoreCommentsinstances that represent “continue this thread” links unfortunately appear to have 0 children (default:
A list of
MoreCommentsinstances that were not replaced.
asyncprawcore.TooManyRequestswhen used concurrently.
For example, to replace up to 32
MoreCommentsinstances of a submission try:
submission = await reddit.submission("3hahrw", fetch=False) await submission.comments.replace_more()
Alternatively, to replace
MoreCommentsinstances within the replies of a single comment try:
comment = await reddit.comment("d8r4im1", fetch=False) await comment.refresh() await comment.replies.replace_more()
This method can take a long time as each replacement will discover at most 100 new
Commentinstances. As a result, consider looping and handling exceptions until the method returns successfully. For example:
while True: try: await submission.comments.replace_more() break except PossibleExceptions: print("Handling replace_more exception") await asyncio.sleep(1)
If this method is called, and the comments are refreshed, calling this method again will result in a