Comment

class praw.models.Comment(reddit: Reddit, id: Optional[str] = None, url: Optional[str] = None, _data: Optional[Dict[str, Any]] = None)

A class that represents a reddit comments.

Typical Attributes

This table describes attributes that typically belong to objects of this class. Since attributes are dynamically provided (see Determine Available Attributes of an Object), there is not a guarantee that these attributes will always be present, nor is this list comprehensive in any way.

Attribute Description
author Provides an instance of Redditor.
body The body of the comment.
created_utc Time the comment was created, represented in Unix Time.
distinguished Whether or not the comment is distinguished.
edited Whether or not the comment has been edited.
id The ID of the comment.
is_submitter Whether or not the comment author is also the author of the submission.
link_id The submission ID that the comment belongs to.
parent_id The ID of the parent comment. If it is a top-level comment, this returns the submission ID instead (prefixed with ‘t3’).
permalink A permalink for the comment. Comment objects from the inbox have a context attribute instead.
replies Provides an instance of CommentForest.
score The number of upvotes for the comment.
stickied Whether or not the comment is stickied.
submission Provides an instance of Submission. The submission that the comment belongs to.
subreddit Provides an instance of Subreddit. The subreddit that the comment belongs to.
subreddit_id The subreddit ID that the comment belongs to.
__init__(reddit: Reddit, id: Optional[str] = None, url: Optional[str] = None, _data: Optional[Dict[str, Any]] = None)

Construct an instance of the Comment object.

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()
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 cheating. [Ref]

Example usage:

submission = reddit.submission(id='5or86n')
submission.clear_vote()

comment = reddit.comment(id='dxolpyc')
comment.clear_vote()
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 uncollapse()

delete()

Delete the object.

Example usage:

comment = reddit.comment('dkk4qjd')
comment.delete()

submission = reddit.submission('8dmv8z')
submission.delete()
disable_inbox_replies()

Disable inbox replies for the item.

Example usage:

comment = reddit.comment('dkk4qjd')
comment.disable_inbox_replies()

submission = reddit.submission('8dmv8z')
submission.disable_inbox_replies()

See also enable_inbox_replies()

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 cheating. [Ref]

Example usage:

submission = reddit.submission(id='5or86n')
submission.downvote()

comment = reddit.comment(id='dxolpyc')
comment.downvote()

See also upvote()

edit(body)

Replace the body of the object with body.

Parameters:body – The Markdown formatted content for the updated object.
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.

Example usage:

comment = reddit.comment('dkk4qjd')
comment.enable_inbox_replies()

submission = reddit.submission('8dmv8z')
submission.enable_inbox_replies()

See also disable_inbox_replies()

fullname

Return the object’s fullname.

A fullname is an object’s kind mapping like t3 followed by an underscore and the object’s base36 ID, e.g., t1_c5s96e0.

gild()

Gild the author of the item.

Note

Requires the authenticated user to own Reddit Coins. Calling this method will consume Reddit Coins.

Example usage:

comment = reddit.comment('dkk4qjd')
comment.gild()

submission = reddit.submission('8dmv8z')
submission.gild()
static id_from_url(url: str) → str

Get the ID of a comment from the full URL.

is_root

Return True when the comment is a top level comment.

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 mark_unread()

To mark the whole inbox as read with a single network request, use praw.models.Inbox.mark_read()

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 mark_read()

mod

Provide an instance of CommentModeration.

parent() → Union[_Comment, Submission]

Return the parent of the comment.

The returned parent will be an instance of either Comment, or Submission.

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'), or reddit.inbox.comment_replies, then the returned parent may be a lazy instance of either Comment, or Submission.

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 a Submission. 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 Comment ancestor you can chain successive calls to parent() with calls to refresh() at every 9 levels. For example:

comment = reddit.comment('dkk4qjd')
ancestor = comment
refresh_counter = 0
while not ancestor.is_root:
    ancestor = ancestor.parent()
    if refresh_counter % 9 == 0:
        ancestor.refresh()
    refresh_counter += 1
print('Top-most Ancestor: {}'.format(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.

classmethod parse(data: Dict[str, Any], reddit: Reddit) → Any

Return an instance of cls from data.

Parameters:
  • data – The structured data.
  • reddit – An instance of Reddit.
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()
replies

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_sort and reply_limit attributes before replies are fetched, including any call to refresh():

comment.reply_sort = 'new'
comment.refresh()
replies = comment.replies
reply(body)

Reply to the object.

Parameters:body – The Markdown formatted content for a comment.
Returns:A Comment object for the newly created comment or None if Reddit doesn’t provide one.

A None value can be returned if the target is a comment or submission in a quarantined subreddit and the authenticated user has not opt-ed in to viewing the content. When this happens the comment will be sucessfully created on Reddit and can be retried by drawing the comment from the user’s comment history.

Example usage:

submission = reddit.submission(id='5or86n')
submission.reply('reply')

comment = reddit.comment(id='dxolpyc')
comment.reply('reply')
report(reason)

Report this object to the moderators of its subreddit.

Parameters:reason – The reason for reporting.

Raises APIException if reason is longer than 100 characters.

Example usage:

submission = reddit.submission(id='5or86n')
submission.report('report reason')

comment = reddit.comment(id='dxolpyc')
comment.report('report reason')
save(category=None)

Save the object.

Parameters:category – (Premium) The category to save to. If your user does not have Reddit Premium this value is ignored by Reddit (default: None).

Example usage:

submission = reddit.submission(id='5or86n')
submission.save(category="view later")

comment = reddit.comment(id='dxolpyc')
comment.save()

See also unsave()

submission

Return the Submission object 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 collapse()

unsave()

Unsave the object.

Example usage:

submission = reddit.submission(id='5or86n')
submission.unsave()

comment = reddit.comment(id='dxolpyc')
comment.unsave()

See also save()

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 cheating. [Ref]

Example usage:

submission = reddit.submission(id='5or86n')
submission.upvote()

comment = reddit.comment(id='dxolpyc')
comment.upvote()

See also downvote()

Note

This list of attributes is not complete. PRAW dynamically provides the attributes that Reddit returns via the API. Because those attributes are subject to change on Reddit’s end, PRAW makes no effort to document them, other than to instruct you on how to discover what is available. See Determine Available Attributes of an Object for detailed information.