Comment¶
- class praw.models.Comment(reddit, id=None, url=None, _data=None)¶
A class that represents a Reddit comment.
Typical Attributes
Note
This table describes attributes that typically belong to objects of this class. PRAW dynamically provides the attributes that Reddit returns via the API. Since those attributes are subject to change on Reddit’s end, PRAW makes no effort to document any new/removed/changed attributes, other than to instruct you on how to discover what is available. As a result, this table of attributes may not be complete. See Determine Available Attributes of an Object for detailed information.
If you would like to add an attribute to this table, feel free to open a pull request.
Attribute
Description
authorProvides an instance of
Redditor.bodyThe body of the comment, as Markdown.
body_htmlThe body of the comment, as HTML.
created_utcTime the comment was created, represented in Unix Time.
distinguishedWhether or not the comment is distinguished.
editedWhether or not the comment has been edited.
idThe ID of the comment.
is_submitterWhether or not the comment author is also the author of the submission.
likesThe user’s current vote status on the comment:
Trueif upvoted,Falseif downvoted, andNoneif not voted or not logged in.link_idThe submission ID that the comment belongs to.
parent_idThe ID of the parent comment (prefixed with
t1_). If it is a top-level comment, this returns the submission ID instead (prefixed witht3_).permalinkA permalink for the comment.
Commentobjects from the inbox have acontextattribute instead.repliesProvides an instance of
CommentForest.savedWhether or not the comment is saved.
scoreThe number of upvotes for the comment.
stickiedWhether or not the comment is stickied.
submissionProvides an instance of
Submission. The submission that the comment belongs to.subredditProvides an instance of
Subreddit. The subreddit that the comment belongs to.subreddit_idThe subreddit ID that the comment belongs to.
- Parameters:
reddit (praw.Reddit)
id (str | None)
url (str | None)
- block()¶
Block the user who sent the item.
Note
This method pertains only to objects which were retrieved via the inbox.
Example usage:
comment = reddit.comment("dkk4qjd") comment.block() # or, identically: comment.author.block()
- Return type:
- clear_vote()¶
Clear the authenticated user’s vote on the object.
Note
Votes must be cast by humans. That is, API clients proxying a human’s action one-for-one are OK, but bots deciding how to vote on content or amplifying a human’s vote are not. See the reddit rules for more details on what constitutes vote manipulation. [Ref]
Example usage:
submission = reddit.submission("5or86n") submission.clear_vote() comment = reddit.comment("dxolpyc") comment.clear_vote()
- Return type:
- collapse()¶
Mark the item as collapsed.
Note
This method pertains only to objects which were retrieved via the inbox.
Example usage:
inbox = reddit.inbox() # select first inbox item and collapse it message = next(inbox) message.collapse()
See also
- Return type:
- delete()¶
Delete the object.
Example usage:
comment = reddit.comment("dkk4qjd") comment.delete() submission = reddit.submission("8dmv8z") submission.delete()
- Return type:
- disable_inbox_replies()¶
Disable inbox replies for the item.
Note
This can only apply to items created by the authenticated user.
Example usage:
comment = reddit.comment("dkk4qjd") comment.disable_inbox_replies() submission = reddit.submission("8dmv8z") submission.disable_inbox_replies()
See also
- Return type:
- downvote()¶
Downvote the object.
Note
Votes must be cast by humans. That is, API clients proxying a human’s action one-for-one are OK, but bots deciding how to vote on content or amplifying a human’s vote are not. See the reddit rules for more details on what constitutes vote manipulation. [Ref]
Example usage:
submission = reddit.submission("5or86n") submission.downvote() comment = reddit.comment("dxolpyc") comment.downvote()
See also
- Return type:
- edit(body)¶
Replace the body of the object with
body.- Parameters:
body (
str) – The Markdown formatted content for the updated object.- Return type:
- Returns:
The current instance after updating its attributes.
Example usage:
comment = reddit.comment("dkk4qjd") # construct the text of an edited comment # by appending to the old body: edited_body = comment.body + "Edit: thanks for the gold!" comment.edit(edited_body)
- enable_inbox_replies()¶
Enable inbox replies for the item.
Note
This can only apply to items created by the authenticated user.
Example usage:
comment = reddit.comment("dkk4qjd") comment.enable_inbox_replies() submission = reddit.submission("8dmv8z") submission.enable_inbox_replies()
See also
- Return type:
- property fullname: str¶
Return the object’s fullname.
A fullname is an object’s kind mapping like
t3followed by an underscore and the object’s base36 ID, e.g.,t1_c5s96e0.
- static id_from_url(url)¶
Get the ID of a comment from the full URL.
- mark_read()¶
Mark a single inbox item as read.
Note
This method pertains only to objects which were retrieved via the inbox.
Example usage:
inbox = reddit.inbox.unread() for message in inbox: # process unread messages ...
See also
To mark the whole inbox as read with a single network request, use
Inbox.mark_all_read()- Return type:
- mark_unread()¶
Mark the item as unread.
Note
This method pertains only to objects which were retrieved via the inbox.
Example usage:
inbox = reddit.inbox(limit=10) for message in inbox: # process messages ...
See also
- Return type:
- mod()¶
Provide an instance of
CommentModeration.Example usage:
comment = reddit.comment("dkk4qjd") comment.mod.approve()
- Return type:
- parent()¶
Return the parent of the comment.
The returned parent will be an instance of either
Comment, orSubmission.If this comment was obtained through a
Submission, then its entire ancestry should be immediately available, requiring no extra network requests. However, if this comment was obtained through other means, e.g.,reddit.comment("COMMENT_ID"), orreddit.inbox.comment_replies, then the returned parent may be a lazy instance of eitherComment, orSubmission.Lazy comment example:
comment = reddit.comment("cklhv0f") parent = comment.parent() # 'replies' is empty until the comment is refreshed print(parent.replies) # Output: [] parent.refresh() print(parent.replies) # Output is at least: [Comment(id="cklhv0f")]
Warning
Successive calls to
parent()may result in a network request per call when the comment is not obtained through aSubmission. See below for an example of how to minimize requests.If you have a deeply nested comment and wish to most efficiently discover its top-most
Commentancestor you can chain successive calls toparent()with calls torefresh()at every 9 levels. For example:ancestor = reddit.comment("dkk4qjd") refresh_counter = 0 while not ancestor.is_root: ancestor = ancestor.parent() if refresh_counter % 9 == 0: ancestor.refresh() refresh_counter += 1 print(f"Top-most Ancestor: {ancestor}")
The above code should result in 5 network requests to Reddit. Without the calls to
refresh()it would make at least 31 network requests.- Return type:
- classmethod parse(data, reddit)¶
Return an instance of
clsfromdata.
- refresh()¶
Refresh the comment’s attributes.
If using
Reddit.comment()this method must be called in order to obtain the comment’s replies.Example usage:
comment = reddit.comment("dkk4qjd") comment.refresh()
- Return type:
- property replies: CommentForest¶
Provide an instance of
CommentForest.This property may return an empty list if the comment has not been refreshed with
refresh()Sort order and reply limit can be set with the
reply_sortandreply_limitattributes before replies are fetched, including any call torefresh():comment.reply_sort = "new" comment.refresh() replies = comment.replies
Note
The appropriate values for
reply_sortinclude"confidence","controversial","new","old","q&a", and"top".
- reply(body)¶
Reply to the object.
- Parameters:
body (
str) – The Markdown formatted content for a comment.- Return type:
- Returns:
A
CommentorMessageobject for the newly created comment or message orNoneif Reddit doesn’t provide one.- Raises:
prawcore.exceptions.Forbiddenwhen attempting to reply to some items, such as locked submissions/comments or non-replyable messages.
A
Nonevalue can be returned if the target is a comment or submission in a quarantined subreddit and the authenticated user has not opt-ed into viewing the content. When this happens the comment will be successfully created on Reddit and can be retried by drawing the comment from the user’s comment history.Example usage:
submission = reddit.submission("5or86n") submission.reply("reply") comment = reddit.comment("dxolpyc") comment.reply("reply")
- report(reason)¶
Report this object to the moderators of its subreddit.
- Parameters:
reason (
str) – The reason for reporting.- Raises:
RedditAPIExceptionifreasonis longer than 100 characters.- Return type:
Example usage:
submission = reddit.submission("5or86n") submission.report("report reason") comment = reddit.comment("dxolpyc") comment.report("report reason")
- save(*, category=None)¶
Save the object.
- Parameters:
category (
str|None) – The category to save to. If the authenticated user does not have Reddit Premium this value is ignored by Reddit (default:None).- Return type:
Example usage:
submission = reddit.submission("5or86n") submission.save(category="view later") comment = reddit.comment("dxolpyc") comment.save()
See also
- property submission: Submission¶
Return the
Submissionobject this comment belongs to.
- uncollapse()¶
Mark the item as uncollapsed.
Note
This method pertains only to objects which were retrieved via the inbox.
Example usage:
inbox = reddit.inbox() # select first inbox item and uncollapse it message = next(inbox) message.uncollapse()
See also
- Return type:
- unsave()¶
Unsave the object.
Example usage:
submission = reddit.submission("5or86n") submission.unsave() comment = reddit.comment("dxolpyc") comment.unsave()
See also
- Return type:
- upvote()¶
Upvote the object.
Note
Votes must be cast by humans. That is, API clients proxying a human’s action one-for-one are OK, but bots deciding how to vote on content or amplifying a human’s vote are not. See the reddit rules for more details on what constitutes vote manipulation. [Ref]
Example usage:
submission = reddit.submission("5or86n") submission.upvote() comment = reddit.comment("dxolpyc") comment.upvote()
See also
- Return type: