I understand that you can view things within Python as attributes as per this article. But I am curious as to why in an instance of a class, parent_instance
, we do not have the same attributes stored within the ParentClass
. This question said that, "The '__dict__'
and '__weakref__'
entries in a class's __dict__
(when present) are descriptors used for retrieving an instance's dict pointer and weakref pointer"
I know that with a dir(parent_instace)
call, I can still obtain a '__weakref__'
, which will be None, and the method_1
and method_2
. But why aren't these within __dict__
in a similar format only set to None?
What is setting or defining what is in __dict__
? Why can I not access method attributes within an instance using __dict__
. I understand it's not within the scope, but why is it not in the scope? Shouldn't the method attributes be within the instance since it can access them?
Reference Code:
class ParentClass:
def __init__(self):
self.int_1: int = 0
self.str_1: str = ''
def method_1(self):
pass
def method_2(self):
pass
parent_instance = ParentClass()
print("Parent Class Attributes:")
for atr in ParentClass.__dict__:
print(atr)
print("\nParent Instance Attributes:")
for atr in parent_instance.__dict__:
print(atr)