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 9223625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:07:01+00:00 2026-06-18T04:07:01+00:00

Consider this: >>> import math >>> dir(math) [‘__doc__’, ‘__name__’, ‘__package__’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’,

  • 0

Consider this:

>>> import math
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

__dict__ is not listed but I can still do

>>> math.__dict__

Same thing with

>>> class MyClass(object):
         def __init__(self, var1, var2):

             self.a = var1
             self.b = var2

         def __call__(self, arg):

             print " The arg passed is ", arg

>>> dir(MyClass) 

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

__bases__ and __base__ is not listed but I can still do:

>>> MyClass.__bases__
(<type 'object'>,)
>>> MyClass.__base__
<type 'object'>

Question:

How can I know or tell in advance all the attributes reachable from a python object? In this case how can I tell that math module has __dict__ and MyClass has __base__ and __bases__ ?

  • 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-18T04:07:02+00:00Added an answer on June 18, 2026 at 4:07 am

    If you’re looking for something completely general, there is no way to do it, because attribute lookup is dynamic. As the docs say:

    attempt to return a list of valid attributes for that object.

    The key word there is “attempt”. Down below:

    Note Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.

    Effectively, all it can do is look at the __dict__ of the object, its type, and any base classes of the type. (And then it subtracts out a few things—e.g., attributes of the type of the type are skipped to avoid exposing metaclass attributes, which you usually don’t care about). If you want the exact details, read the documentation linked above.

    Meanwhile, if the object has __getattr__/__setattr__/__delattr__, or __getattribute__, or C-API equivalents of the above, it may have all kinds of attributes that aren’t in any dictionary, maybe even an infinite set of them. For example:

    class C(object):
        def __getattr__(self, name):
            return name
    c = C()
    print(c.a)
    

    This will print out a. But how could dir possibly know that c.a exists? Even if it could interpret my code, it would have to print out a list of all possible strings, which you obviously don’t want.

    There are also special attribute names that are handled in fancy ways under the covers by the interpreter.

    Also, any object can define a __dir__ method to return whatever it wants.

    If you’re looking for something specific, you can probably do that. For example, if you’re just trying to get the attributes without subtracting out metaclass attributes, that’s just dir(x) + dir(type(x)), or, maybe better, dir(x) + [a for a in dir(type(x)) if a not in dir(x)] (to remove duplicates).

    But if you want to know all attributes for an object, well, you can’t.

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

Sidebar

Related Questions

Consider this code: import socket store = [] scount = 0 while True: scount+=1
Consider this python program: import sys lc = 0 for line in open(sys.argv[1]): lc
Consider this simple AS3 class. package { import flash.display.Sprite; import flash.display.MovieClip; public class MySprite
Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;
please consider this code: import xml.etree.ElementTree as ET import urllib XML_response = urllib.urlopen('http://www.navlost.eu/aero/metar/?icao=LWSK&dt0=2011-05-03+12%3A00%3A00&c=1&rt=metar').read() tree
Consider this code: import java.util.*; class jm45 implements Comparator<jm45> { private int x; jm45(int
Consider this code: import java.util.regex.*; public class Pattern3 { /** * @param args the
Consider this small test class: import java.util.List; public abstract class Test { // CAN
Duplicate of: What does if __name__== __main__ do? Consider this code: if __name__ ==
Scipy version 0.10.0 Consider the following: >>> import math >>> from scipy.optimize import fsolve

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.