CommentForest

class asyncpraw.models.comment_forest.CommentForest(submission: asyncpraw.models.Submission, comments: Optional[List[asyncpraw.models.Comment]] = 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 index in the list.

This method is to be used like an array access, such as:

first_comment = submission.comments[0]

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: Optional[List[asyncpraw.models.Comment]] = None)

Initialize a CommentForest instance.

Parameters
  • submission – An instance of Submission that is the parent of the comments.

  • comments – Initialize the Forest with a list of comments (default: None).

__len__() int

Return the number of top-level comments in the forest.

list() Union[List[Union[asyncpraw.models.Comment, asyncpraw.models.MoreComments]], Coroutine[Any, Any, List[Union[asyncpraw.models.Comment, asyncpraw.models.MoreComments]]]]

Return a flattened list of all Comments.

This list may contain MoreComments instances if replace_more() was not called first.

await replace_more(limit: int = 32, threshold: int = 0) List[asyncpraw.models.MoreComments]

Update the comment forest by resolving instances of MoreComments.

Parameters
  • limit – The maximum number of MoreComments instances to replace. Each replacement requires 1 API request. Set to None to have no limit, or to 0 to remove all MoreComments instances without additional requests (default: 32).

  • threshold – The minimum number of children comments a MoreComments instance must have in order to be replaced. MoreComments instances that represent “continue this thread” links unfortunately appear to have 0 children (default: 0).

Returns

A list of MoreComments instances that were not replaced.

Raises

asyncprawcore.TooManyRequests when used concurrently.

For example, to replace up to 32 MoreComments instances of a submission try:

submission = await reddit.submission("3hahrw", fetch=False)
await submission.comments.replace_more()

Alternatively, to replace MoreComments instances within the replies of a single comment try:

comment = await reddit.comment("d8r4im1", fetch=False)
await comment.refresh()
await comment.replies.replace_more()

Note

This method can take a long time as each replacement will discover at most 100 new Comment instances. 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)

Warning

If this method is called, and the comments are refreshed, calling this method again will result in a DuplicateReplaceException.