Property setter with multiple values

后端 未结 5 1141
星月不相逢
星月不相逢 2020-12-09 11:55

I have a property setter which generates a unique id by taking two strings and hashing it:

@id.setter
def id(self,value1,value2):
    self._id = sha512(value         


        
5条回答
  •  無奈伤痛
    2020-12-09 12:01

    You could use the property() function as well like this.

    from hashlib import sha512
    
    class U:
    
        def __init__(self, value1, value2):
            self.id_setter(value1, value2)
    
        def id_getter(self):
            return self._id
    
        def id_setter(self, value1: str, value2: str):
        value1, value2 = value1.encode(), value2.encode()
        self._id = sha512(value1+value2)
    
        id = property(id_getter, id_setter) # <----
    
    u = U("Foo", "Bar")
    print(u.id.hexdigest())
    

提交回复
热议问题