Skip to content
Back to top
View this page

# SubredditRules

class praw.models.reddit.rules.SubredditRules(subreddit: praw.models.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("test").rules:
          print(rule)

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

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

  __call__() → List[praw.models.Rule]

    Return a list of `Rule`s (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: str | int | slice) → praw.models.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 lazily loaded, so you might have to access an attribute to get all the expected attributes.

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

        rule_name = "No spam"
        rule = reddit.subreddit("test").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 r/test:

        rule = reddit.subreddit("test").rules[1]

  __init__(subreddit: praw.models.Subreddit)

    Initialize a `SubredditRules` instance.

    Parameters:

      subreddit – The subreddit whose rules to work with.

  __iter__() → Iterator[praw.models.Rule]

    Iterate through the rules of the subreddit.

    Returns:

      An iterator containing all the rules of a subreddit.

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

    For example, to get the rules for r/test:

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

  mod() → SubredditRulesModeration

    Contain methods to moderate subreddit rules as a whole.

    To add rule `"No spam"` to r/test try:

        reddit.subreddit("test").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 r/test:

        subreddit = reddit.subreddit("test")
        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)