SubredditRules

class praw.models.reddit.rules.SubredditRules(subreddit: Subreddit)

Provide a set of functions to access a Subreddit’s rules.

For example, to list all the rules for a subreddit:

for rule in reddit.subreddit("AskReddit").rules:
    print(rule)

Moderators can also add rules to the subreddit. For example, to make a rule called "No spam" in the subreddit "NAME":

reddit.subreddit("NAME").rules.mod.add(
    short_name="No spam",
    kind="all",
    description="Do not spam. Spam bad")
__call__() → List[praw.models.reddit.rules.Rule]

Return a list of Rules (Deprecated).

Returns:A list of instances of Rule.

Deprecated since version 7.1: Use the iterator by removing the call to SubredditRules. For example, in order to use the iterator:

for rule in reddit.subreddit("test").rules:
    print(rule)
__getitem__(short_name: Union[str, int, slice]) → praw.models.reddit.rules.Rule

Return the Rule for the subreddit with short_name short_name.

Parameters:short_name – The short_name of the rule, or the rule number.

Note

Rules fetched using a specific rule name are lazy loaded, so you might have to access an attribute to get all of the expected attributes.

This method is to be used to fetch a specific rule, like so:

rule_name = "No spam"
rule = reddit.subreddit("NAME").rules[rule_name]
print(rule)

You can also fetch a numbered rule of a subreddit.

Rule numbers start at 0, so the first rule is at index 0, and the second rule is at index 1, and so on.

Raises:IndexError if a rule of a specific number does not exist.

Note

You can use negative indexes, such as -1, to get the last rule. You can also use slices, to get a subset of rules, such as the last three rules with rules[-3:].

For example, to fetch the second rule of AskReddit:

rule = reddit.subreddit("AskReddit").rules[1]
__init__(subreddit: Subreddit)

Create a SubredditRules instance.

Parameters:subreddit – The subreddit whose rules to work with.
__iter__() → Iterator[praw.models.reddit.rules.Rule]

Iterate through the rules of the subreddit.

Returns:An iterator containing all of the rules of a subreddit.

This method is used to discover all rules for a subreddit.

For example, to get the rules for the subreddit "NAME":

for rule in reddit.subreddit("NAME").rules:
    print(rule)
mod

Contain methods to moderate subreddit rules as a whole.

To add rule "No spam" to the subreddit "NAME" try:

reddit.subreddit("NAME").rules.mod.add(
     short_name="No spam",
     kind="all",
     description="Do not spam. Spam bad")

To move the fourth rule to the first position, and then to move the prior first rule to where the third rule originally was in the subreddit "NAME":

subreddit = reddit.subreddit("NAME")
rules = list(subreddit.rules)
new_rules = rules[3:4] + rules[1:3] + rules[0:1] + rules[4:]
# Alternate: [rules[3]] + rules[1:3] + [rules[0]] + rules[4:]
new_rule_list = subreddit.rules.mod.reorder(new_rules)