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

  • Home
  • SEARCH
  • 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 8765897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:24:21+00:00 2026-06-13T16:24:21+00:00

I found the following function, but I have no idea why additional __bases__ scan

  • 0

I found the following function, but I have no idea why additional __bases__ scan is required:

def getMembersWithBases(classType):
    members = set(dir(classType))
    # recursive bases scan
    for baseClassType in classType.__bases__:
        members.update(getMembersWithBases(baseClassType))
    return members

The following function is faster and gives same results – so why is the additional __bases__ scan needed at al?

def getMembers(classType):
    members = set(dir(classType))
    return members

Some test code with both new- and old-style classes:

class Father(object):
    def testFather():
        pass

class Mother(object):
    def testMother():
        pass

class Child(Father, Mother):
    def testChild():
        pass

print type(Child)
print getMembers(Child) == getMembersWithBases(Child)                                             

class Father:
    def testFather():
        pass

class Mother:
    def testMother():
        pass

class Child(Father, Mother):
    def testChild():
        pass

print type(Child)
print getMembers(Child) == getMembersWithBases(Child)

Result:

<type 'type'>
True
<type 'classobj'>
True
  • 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-13T16:24:25+00:00Added an answer on June 13, 2026 at 4:24 pm

    Indeed, the dir() function, for classes, already includes all the classes listed in __bases__:

    If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.

    However, it is possible for a class to override that behaviour by specifying a __dir__ method (via a metaclass, a classmethod is not enough):

    If the object has a method named __dir__(), this method will be called and must return the list of attributes.

    When a __dir__() method is present, __bases__ is not recursed over:

    >>> class Foo(object):
    ...     def spam(self): pass
    ... 
    >>> class Bar(object):
    ...     def ham(self): pass
    ... 
    >>> class CustomDirMetaclass(type):
    ...     def __dir__(cls):
    ...         return ['eggs']
    ... 
    >>> class Baz(Foo, Bar):
    ...     __metaclass__ = CustomDirMetaclass
    ...     def eggs(self): pass
    ... 
    >>> dir(Baz)
    ['eggs']
    >>> getMembers(Baz)
    set(['eggs'])
    >>> getMembersWithBases(Baz)
    set(['__module__', '__getattribute__', 'eggs', '__reduce__', '__subclasshook__', '__dict__', '__sizeof__', '__weakref__', '__init__', 'ham', '__setattr__', '__reduce_ex__', '__new__', 'spam', '__format__', '__class__', '__doc__', '__delattr__', '__repr__', '__hash__', '__str__'])
    

    Thus, the explicit recursion over __bases__ in the getMembersWithBases() class method could be an attempt at bypassing any custom __dir__() implementations.

    Otherwise, the recursion over __bases__ is completely redundant.

    In my personal opinion, the recursion over __bases__ is redundant even if there are __dir__() methods present in the class hierarchy. In such cases, the __dir__() method override is at fault as that method should recurse over the classes listed in __bases__ to correctly mimic the behaviour of the dir() function.

    To be honest, I suspect that the author of the function that you found was not aware of the recursive nature of dir(), and added the __bases__ recursion needlesly, not as a means to bypass custom __dir__() methods.

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

Sidebar

Related Questions

I found the following lines in a makefile tutorial, but I have some problem
I have the following function written in Go. The idea is the function has
I have no idea if I just found a potential jQuery bug, but check
In the book Programming Collective Intelligence I found the following function to compute the
I used the following function I found online and it works perfectly. However, when
I found the following code in my team's project: Public Shared Function isRemoteDisconnectMessage(ByRef m
On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string
I have a matlab function img_process that requires the following parameters : image_name intensity
I have the following code in a class: def __setattr__(self, key, value): self.__dict__['d'][key] =
Recently, I have been getting familiar with PostgreSQL(using 8.2) and found the date_trunc function

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.