attrs
attrs copied to clipboard
attr.s(frozen=True) breaks writeable properties
(txkube) exarkun@baryon:~/Work/LeastAuthority/txkube$ cat brokenattrs.py
import attr
@attr.s(frozen=True)
class X(object):
y = attr.ib()
@property
def z(self):
return 3
@z.setter
def z(self, value):
print("z=", value)
X(y=None).z = 10
(txkube) exarkun@baryon:~/Work/LeastAuthority/txkube$ python brokenattrs.py
Traceback (most recent call last):
File "brokenattrs.py", line 15, in <module>
X(y=None).z = 10
File "/home/exarkun/Environments/txkube/local/lib/python2.7/site-packages/attr/_make.py", line 201, in _frozen_setattrs
raise FrozenInstanceError()
attr.exceptions.FrozenInstanceError
(txkube) exarkun@baryon:~/Work/LeastAuthority/txkube$
Heh we freeze the classes for good ATM. There seems to be interest in a more…flexible freezing process (c.f. also #133).
I opened an issue for with initial pull request for customizing setattr and delattr for frozen classes here: https://github.com/python-attrs/attrs/issues/378
I'm having trouble understanding the solution to this. Is this still not supported or am I missing something? What I ultimately want to do is be able to set a single attribute of a class that has inherited a frozen status. Is this even possible?
you can always set any attribute using object.__setattr__
. An example is in https://www.attrs.org/en/stable/init.html#post-init
you can always set any attribute using
object.__setattr__
. An example is in https://www.attrs.org/en/stable/init.html#post-init
I have started doing that in a method for the frozen class, but I really would prefer this:
class_instance.attribute = value
over this: class_instance.change_attribute(value)
I guess I should have mentioned that. I appreciate the response, but I'm still uncertain if what I want is even doable right now.