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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:07:20+00:00 2026-06-17T09:07:20+00:00

class C(object): def f(self): print self.__dict__ print dir(self) c = C() c.f() output: {}

  • 0
class C(object):
    def f(self):
        print self.__dict__
        print dir(self)
c = C()
c.f()

output:

{}

['__class__', '__delattr__','f',....]

why there is not a ‘f’ in self.__dict__

  • 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-17T09:07:21+00:00Added an answer on June 17, 2026 at 9:07 am

    dir() does much more than look up __dict__

    First of all, dir() is a API method that knows how to use attributes like __dict__ to look up attributes of an object.

    Not all objects have a __dict__ attribute though. For example, if you were to add a __slots__ attribute to your custom class, instances of that class won’t have a __dict__ attribute, yet dir() can still list the available attributes on those instances:

    >>> class Foo(object):
    ...     __slots__ = ('bar',)
    ...     bar = 'spam'
    ... 
    >>> Foo().__dict__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Foo' object has no attribute '__dict__'
    >>> dir(Foo())
    ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'bar']
    

    The same applies to many built-in types; lists do not have a __dict__ attribute, but you can still list all the attributes using dir():

    >>> [].__dict__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'list' object has no attribute '__dict__'
    >>> dir([])
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    

    What dir() does with instances

    Python instances have their own __dict__, but so does their class:

    >>> class Foo(object):
    ...     bar = 'spam'
    ... 
    >>> Foo().__dict__
    {}
    >>> Foo.__dict__.items()
    [('__dict__', <attribute '__dict__' of 'Foo' objects>), ('__weakref__', <attribute '__weakref__' of 'Foo' objects>), ('__module__', '__main__'), ('bar', 'spam'), ('__doc__', None)]
    

    The dir() method uses both these __dict__ attributes, and the one on object to create a complete list of available attributes on the instance, the class, and on all ancestors of the class.

    When you set attributes on a class, instances see these too:

    >>> f = Foo()
    >>> f.ham
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Foo' object has no attribute 'ham'
    >>> Foo.ham = 'eggs'
    >>> f.ham
    'eggs'
    

    because the attribute is added to the class __dict__:

    >>> Foo.__dict__['ham']
    'eggs'
    >>> f.__dict__
    {}
    

    Note how the instance __dict__ is left empty. Attribute lookup on Python objects follows the hierarchy of objects from instance to type to parent classes to search for attributes.

    Only when you set attributes directly on the instance, will you see the attribute reflected in the __dict__ of the instance, while the class __dict__ is left unchanged:

    >>> f.stack = 'overflow'
    >>> f.__dict__
    {'stack': 'overflow'}
    >>> 'stack' in Foo.__dict__
    False
    

    TLDR; or the summary

    dir() doesn’t just look up an object’s __dict__ (which sometimes doesn’t even exist), it will use the object’s heritage (its class or type, and any superclasses, or parents, of that class or type) to give you a complete picture of all available attributes.

    An instance __dict__ is just the ‘local’ set of attributes on that instance, and does not contain every attribute available on the instance. Instead, you need to look at the class and the class’s inheritance tree too.

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

Sidebar

Related Questions

I have two classes: class Base(object): def __init__(self): object.__init__(self) def print_methods(self): print self.__dict__ class
Why does the following: class A(object): def __init__(self, var=[]): self._var = var print 'var
class HelloWorld(object): def say_it(self): return 'Hello I am Hello World' def i_call_hello_world(hw_obj): print 'here...
class C: def func(self, a): print(a) c = C() print(c.__dict__) # {} c.func =
In [5]: class a(object): ...: def __init__(self): ...: print In class a ...: self.a
What I am trying to achieve is something like this: class object: def __init__(self):
class Pen(object): def __init__(self, mean_radius = None, length = None): self.usage = Is used
class Node(object): def __init__(self, data): self.data = data if __name__ == __main__: a =
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
I create the following class: class Image(object): def __init__(self, extension, data, urls=None, user_data=None): self._extension

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.