Python 更改ATTR中冻结类的属性

Python 更改ATTR中冻结类的属性,python,python-attrs,Python,Python Attrs,我希望找到一种方法,在url初始化为特定值时更改url,而不丢失该类的冻结属性,这是否可行?更改传入的值就是a的用途: 更改传入值是a的作用: import attr @attr.s(slots=True, frozen=True) class C: url = attr.ib(type=str) x = attr.ib() y = attr.ib() z = attr.ib(default=10) @y.default def _any

我希望找到一种方法,在url初始化为特定值时更改url,而不丢失该类的冻结属性,这是否可行?

更改传入的值就是a的用途:


更改传入值是a的作用:

import attr
@attr.s(slots=True, frozen=True)
class C:
    url = attr.ib(type=str)
    x = attr.ib()
    y = attr.ib()
    z = attr.ib(default=10)
    
    @y.default
    def _any_name_except_a_name_of_an_attribute(self):
        return self.x + 1
    
    @url.validator
    def map_url(self, attribute, value):
        if value == "apple":
            self.url = "mango"

print(C(x=4,y=5, url="apple"))
url = attr.ib(type=str, converter=lambda x: 'mango' if x=='apple' else x)