Search code examples
pythonredditpraw

Why is `submission.permalink` with PRAW working - even if I can't find anything about this attribute in the source code?


I was looking for an attribute which contains the submission title which is part of the permalink of a submission. Unfortunately, praw.models.reddit.submission.Submission doesn't seem to contain a permalink attribute, according to PRAW docs http://praw.readthedocs.io/en/latest/search.html?q=permalink&check_keywords=yes&area=default

However, if I run

import praw
print(praw.__version__)
reddit = praw.Reddit(...)
for submission in reddit.subreddit("redditdev").hot(limit=5):
    print(type(submission), submission.permalink)

I'll get

4.4.0
<class 'praw.models.reddit.submission.Submission'> /r/redditdev/comments/77gz1m/new_mobile_friendly_reddit_search_is_now_in_beta/
<class 'praw.models.reddit.submission.Submission'> /r/redditdev/comments/77dkl6/can_anyone_explain_why_my_if_x_in_y_isnt_working/
<class 'praw.models.reddit.submission.Submission'> /r/redditdev/comments/77ci3v/ratelimit_you_are_doing_that_too_much/
<class 'praw.models.reddit.submission.Submission'> /r/redditdev/comments/77bak9/is_there_some_standard_way_to_host_bots/
<class 'praw.models.reddit.submission.Submission'> /r/redditdev/comments/77ci1v/praw_checking_if_my_bot_has_already_posted_in_a/

So my call to permalink works fine, even if grep can't find permalink in source files other than comment.py?!

$ grep -r -i -l "permalink" --include "*.py" /usr/local/lib/python3.6/dist-packages/praw/
/usr/local/lib/python3.6/dist-packages/praw/models/reddit/comment.py

It's the same with the latest version, if you're searching for permalink in the source on GitHub: https://github.com/praw-dev/praw/search?l=Python&q=permalink&type=&utf8=%E2%9C%93

What's going on? Where does the attribute come from?


Solution

  • 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:

    https://praw.readthedocs.io/en/latest/getting_started/quick_start.html#determine-available-attributes-of-an-object

    When you iterate over reddit.subreddit("redditdev").hot(limit=5) PRAW makes a request to https://oauth.reddit.com/r/redditdev/hot/.json?limit=5, which essentially returns the same data as https://www.reddit.com/r/redditdev/hot/.json?limit=5. When visiting the latter link, you should see the permalink attribute appear 5 times, because the request is for 5 submissions. You will also see all the other attributes available for submission objects.