- is there a simple way to prevent setting new class attrs?
- while trying with the following snippet, shouldn't
setattr(Derived, "test1", 1)
call the__setattr__
fromBase
?
class Base:
def __setattr__(self, key, value):
raise PermissionError('in base')
def __init_subclass__(cls, *args, **kwargs):
def _setattr_(inst, key, val):
raise PermissionError('in derived')
cls.__setattr__ = _setattr_
class Derived(Base):
pass
setattr(Derived, "test1", 1)
Derived.__setattr__(Derived, "test2", 2)
Traceback (most recent call last):
.
.
.
PermissionError: in derived
Base.__setattr__(Derived, "test3", 3)
Traceback (most recent call last):
.
.
.
PermissionError: in base
EDIT: this is a duplicate. See below.