question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dir changes attributes of praw.objects.Subreddits in place

See original GitHub issue

Today I noticed that the built-in function dir() seems to change the attributes of praw.objects.Subreddits in place.

screen shot 2016-03-09 at 4 01 22 pm

Could this possibly be the result of a buggy __dir__() implementation in a praw.objects.Subreddit parent class? I honestly have no clue what to make of this.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
voussoircommented, Mar 11, 2016

Okay, must be the difference between Python 2 and 3, that’s an interesting distinction. Does two.__dict__ instead of dir(two) cause the same thing to happen?

If this is something that can be fixed in a clean and version-compatible way, I’d say there’s no reason not to, but I’m not sure how you could change the behavior of something as basic as dir. It appears (in 3.5), that it can be overridden:

>>> class A:
...     def __dir__(self):
...             return [1, 2, 3]
...
>>>
>>> a=A()
>>> dir(a)
[1, 2, 3]

I don’t know what you would put inside that method that achieves all the normal functionality of dir and doesn’t do whatever causes Python 2 to trip the lazy loader. Plus it just seems like something that should never have to be done. You mentioned

if someone is writing a function that checks for the existence of a class method, they would get different answers for ‘description’ in dir(prog)

but I think hasattr is always the correct answer anyway. We should have a very good reason before overriding this kind of thing.

1reaction
bboecommented, Mar 11, 2016
class Test(object):
    def __init__(self):
        self.attribute = True

    def __getattr__(self, attr):
        print('Fetching {}'.format(attr))
        return super(Test, self).__getattr__(attr)


test = Test()
print(dir(test) == dir(test))
python2 ./tmp.py 
Fetching __members__
Fetching __methods__
Fetching __members__
Fetching __methods__
True
python3 ./tmp.py 
True
Read more comments on GitHub >

github_iconTop Results From Across the Web

Submission — PRAW 7.6.1 documentation
This table describes attributes that typically belong to objects of this class. PRAW dynamically provides the attributes that Reddit returns via the API....
Read more >
Subreddit — PRAW 7.6.1 documentation - Read the Docs
This table describes attributes that typically belong to objects of this class. PRAW dynamically provides the attributes that Reddit returns via the API....
Read more >
Change Log — PRAW 3.6.2 documentation
[CHANGE] Fixed the subreddits attribute of praw.objects.Multireddit being returned as a list of dicts. It is now a list of Subreddit objects.
Read more >
Redditor — PRAW 7.6.1 documentation
This table describes attributes that typically belong to objects of this class. PRAW dynamically provides the attributes that Reddit returns via the API....
Read more >
Message — PRAW 7.6.1 documentation
PRAW dynamically provides the attributes that Reddit returns via the API. Since those attributes are subject to change on Reddit's end, PRAW makes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found