CommentForest¶
-
class
praw.models.comment_forest.CommentForest(submission, comments=None)¶ A forest of comments starts with multiple top-level comments.
Each of these comments can be a tree of replies.
-
__getitem__(index)¶ Return the comment at position
indexin 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, comments=None)¶ Initialize a CommentForest instance.
Parameters: - submission – An instance of
Subredditthat is the parent of the comments. - comments – Initialize the Forest with a list of comments (default: None).
- submission – An instance of
-
__len__()¶ Return the number of top-level comments in the forest.
-
list()¶ Return a flattened list of all Comments.
This list may contain
MoreCommentsinstances ifreplace_more()was not called first.
-
replace_more(limit=32, threshold=0)¶ Update the comment forest by resolving instances of MoreComments.
Parameters: - limit – The maximum number of
MoreCommentsinstances to replace. Each replacement requires 1 API request. Set toNoneto have no limit, or to0to remove allMoreCommentsinstances without additional requests (default: 32). - 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: 0).
Returns: A list of
MoreCommentsinstances that were not replaced.For example, to replace up to 32
MoreCommentsinstances of a submission try:submission = reddit.submission('3hahrw') submission.comments.replace_more()
Alternatively, to replace
MoreCommentsinstances within the replies of a single comment try:comment = reddit.comment('d8r4im1') comment.refresh() comment.replies.replace_more()
Note
This method can take a long time as each replacement will discover at most 20 new
CommentorMoreCommentsinstances. As a result, consider looping and handling exceptions until the method returns successfully. For example:while True: try: submission.comments.replace_more() break except PossibleExceptions: print('Handling replace_more exception') sleep(1)
- limit – The maximum number of
-