It had (in my opinion) a silly workaround.
That's why no TypeError
is raised when __slots__
is empty, and having an empty __slots__
attribute preserves the "wondered" python behaviour what warns when assigning to an attribute not defined in __slots__
.
So, consider the following metaclass:
class SlotBase(type):
def __new__(cls,name,bases,dctn):
if ('_slots_' in dctn) and not ('__slots__' in dctn):
dctn['__slots__'] = []
elif '__slots__' in dctn:
for base in bases:
if hasattr(base,'_slots_'):
dctn['__slots__'] += getattr(base,'_slots_')
return super().__new__(cls,name,bases,dctn)
An then deploy on base classes.
class A(metaclass=SlotBase):
_slots_=['slot1'] #fake __slots__ attribute
classPropertyA = 'Some silly value'
def functA(self):
print('I\'m functA')
class B(metaclass=SlotBase):
_slots_=['slot2'] #fake __slots__ attribute
classPropertyB = 'Some other silly value'
def functB(self):
print('I\'m functB')
class C(A,B):
__slots__ = []
classPropertyC = 'Just another silly value'
If we execute following code
c=C()
c.classPropertyC
c.classPropertyA
c.functA()
c.functB()
c.slot1='Slot exists then assignment is accepted'
c.slot3='Slot does not exists then assignment couldn\'t be accepted'
This produces following output
Just another silly value
Some silly value
I'm functA
I'm functB
Traceback (most recent call last):
File "/tmp/slots.py", line 41, in <module>
c.slot3='Slot does not exists then assignment couldn\'t be accepted'
AttributeError: 'C' object has no attribute 'slot3'