Property setter with multiple values

后端 未结 5 1097
星月不相逢
星月不相逢 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())
    
    0 讨论(0)
  • 2020-12-09 12:10

    I don't know if it's what you want, but here is an other way for passing two values or more:

    @idx.setter
    def idx(self, args):
        self._idx = sha512(''.join([str(i) for i in args]))
    

    You can avoid the restriction of list or tuple :

    a.idx = 'zere', 'fezfz'
    a.idx = ("eddez", "erez")
    a.idx = 'deze', 'drezre', 'ezre'
    

    You have to test for exception and handle excpetion in init.

    0 讨论(0)
  • 2020-12-09 12:11

    How do I pass two values to the setter?

    You can pass an iterable(tuple, list) to the setter, for example:

    class A(object):
        def __init__(self, val):
            self.idx = val
    
        @property    
        def idx(self):
            return self._idx
    
        @idx.setter
        def idx(self, val):
            try:
                value1, value2 = val
            except ValueError:
                raise ValueError("Pass an iterable with two items")
            else:
                """ This will run only if no exception was raised """
                self._idx = sha512(value1+value2)
    

    Demo:

    >>> a = A(['foo', 'bar'])     #pass a list
    >>> b = A(('spam', 'eggs'))   #pass a tuple
    >>> a.idx
    <sha512 HASH object @ 0xa57e688>
    >>> a.idx = ('python', 'org')  #works
    >>> b.idx = ('python',)         #fails
    Traceback (most recent call last):
        ...
        raise ValueError("Pass an iterable with two items")
    ValueError: Pass an iterable with two items
    
    0 讨论(0)
  • 2020-12-09 12:14

    The setter can only take one value, so use a tuple: (value1, value2).

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

    (You didn't post what sha512 is. I'm assuming you are using hashlib.sha512, and sha512 is calling the update method, which requires a string as input.)

    0 讨论(0)
  • 2020-12-09 12:26

    I have some doubts regarding the coding practices here. As a user of an API, I'd always assume that whatever I set as a property, I can get it back from the same property. In addition to that, having something called id mutable looks suspicious.

    As for passing two values, how about:

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

    Then assign a tuple:

    myobj.id = (val1, val2)
    
    0 讨论(0)
提交回复
热议问题