Following piece of code lists the attributes of a class named 'A' in a sorted order:-
>>> class A():
def __init__(self, i):
self.at = i
>>> sorted(vars(A))
['__dict__', '__doc__', '__init__', '__module__', '__weakref__']
Now, printing the value of key, '__dict__'
results this:-
>>> vars(A)['__dict__'] #Value of '__dict__'
<attribute '__dict__' of 'A' objects>
As per docs, vars([object])
Return the
__dict__
attribute for a module, class, instance, or any other object with a__dict__
attribute.
What I am not understanding is that is the '__dict__'
attribute in the list the same attribute used by vars()
to return the attributes of A
or is it a different attribute which has some another objective like implementing A's objects' namespace as suggested (according to me) by the value which '__dict__'
holds.
Edit:-
The first part of the question is very much related to this other question (also, mentioned by @eliotness) but it's the second part (described below) for which I can't find any answers or related question and hence, changing title of the question.
Let's consider another code that produces list of attributes of ultimate base class in Python, object
:-
>>> sorted(vars(object))
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', ...., '__str__', '__subclasshook__']
>>> hasattr(object, '__dict__')
True
>>> sorted(getattr(object, '__dict__')) == sorted(vars(object))
True
Another quotation from docs about object.__dict__
A dictionary or other mapping object used to store an object’s (writable) attributes.
This time, '__dict__'
doesn't show up in the list of object
. So, is it that the __dict__
attribute is a read-only attribute in case of object
or any other reason?
Also, is it possible to get a list of read-only attributes in Python in any way?