Listing attributes of namedtuple subclass
The problem is that __slots__
is only limited to a class it is defined in, so base classes will always have their own __dict__
attribute unless you define __slots__
there too. (And also note that the __dict__
attribute of namedtuple
is not a normal dict but a @property.)
From docs:
The action of a
__slots__
declaration is limited to the class where it is defined. As a result, subclasses will have a__dict__
unless they also define__slots__
(which must only contain names of any additional slots).
So, when you defined __slots__
in the subclass then it failed to look for an attribute __dict__
in that class, so moved on to base class where it found the __dict__
attribute.
A simple demo:
class A:
__slots__= ('a', 'b')
@property
def __dict__(self):
print ('inside A')
return self.__slots__
class B(A):
pass
print(B().__dict__)
print ('-'*20)
class B(A):
__slots__ = ()
print(B().__dict__)
output:
{}
--------------------
inside A
()