Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:43:23+00:00 2026-06-15T22:43:23+00:00

Official docs says : If the object has a method named __dir__() , this

  • 0

Official docs says:

If the object has a method named __dir__(), this method will be called
and must return the list of attributes. This allows objects that
implement a custom __getattr__() or __getattribute__() function to
customize the way dir() reports their attributes.

If custom __dir__ implemented, results, returning by another function, inspect.getmembers(), also affected.

For example:

class С(object):
    __slots__ = ['atr']
    def __dir__(self):
        return ['nothing']
    def method(self):
        pass
    def __init__(self):
        self.atr = 'string'

c = C() 
print dir(f) #If we try this - well get ['nothing'] returned by custom __dir__()
print inspect.getmembers(f) #Here we get []
print f.__dict__ #And here - exception will be raised because of __slots__

How in this case list of names of object might be getted?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:43:24+00:00Added an answer on June 15, 2026 at 10:43 pm

    Answer to original question- does inspect.getmembers() use __dir__() like dir() does?

    Here’s the source code for inspect.getmembers() so we can see what it’s really doing:

    def getmembers(object, predicate=None):
        """Return all members of an object as (name, value) pairs sorted by name.                                                                                                                                     
        Optionally, only return members that satisfy a given predicate."""
        results = []
        for key in dir(object):
            try:
                value = getattr(object, key)
            except AttributeError:
                continue
            if not predicate or predicate(value):
                results.append((key, value))
        results.sort()
        return results
    

    From this we see that it is using dir() and just filtering the results a bit.

    How to get attributes with an overridden __dir__()?

    According to this answer, it isn’t possible to always get a complete list of attributes, but we can still definitely get them in some cases/get enough to be useful.

    From the docs:

    If the object does not provide __dir__(), the function tries its best
    to gather information from the object’s __dict__ attribute, if
    defined, and from its type object. The resulting list is not
    necessarily complete, and may be inaccurate when the object has a
    custom __getattr__().

    So if you are not using __slots__, you could look at your object’s __dict__ (and it’s type object’s) to get basically the same info that dir() would normally give you. So, just like with dir(), you would have to use a more rigorous method to get metaclass methods.

    If you are using __slots__, then getting class attributes is, in a way, a bit more simple. Yes, there’s no dict, but there is __slots__ itself, which contains the names of all of the attributes. For example, adding print c.__slots__ to your example code yields ['atr']. (Again, a more rigorous approach is needed to get the attributes of superclasses as well.)

    How to get methods

    You might need a different solution depending on the use case, but if you just want to find out the methods easily, you can simply use the builtin help().

    Modified PyPy dir()

    Here’s an alternative to some of the above: To get a version of dir() that ignores user-defined __dir__ methods, you could just take PyPy’s implementation of dir() and delete the parts that reference __dir__ methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The official docs says that smarty has 'while' support . However I do in
According to the official docs, PyImport_AppendInittab will return -1 on failure. It does not,
The official docs on Jackson views at http://wiki.fasterxml.com/JacksonJsonViews says that you use this kind
Quote from hib official docs: Starting with version 3.0.1, Hibernate added the SessionFactory.getCurrentSession() method.
I have tried to find answer to this question from the official docs, but
I've just read this https://developers.facebook.com/docs/reference/dialogs/feed/ It says since the 12th of July 2011 it
maybe this question is a little basic, but I'm reading the official docs in
according to the official docs there are two options to create parallel collections: 1)
I have read the HOWTO on Unicode from the official docs and a full,
Original question: Note: Below plugin pattern based on the official jQuery docs . I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.