我有一个属性设置器,它通过获取两个字符串并对其进行散列来生成一个唯一的 id:
@id.setter
def id(self,value1,value2):
self._id = sha512(value1+value2)
我有两个问题:
- 是否允许(考虑到良好的 python 编码实践)以这种方式编码
- 如何将两个值传递给 setter?
如何将两个值传递给 setter?
您可以将 iterable(tuple, list) 传递给 setter,例如:
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)
演示:
>>> 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
setter 只能取一个值,因此请使用元组:(value1, value2)
。
@id.setter
def id(self,value):
self._id = sha512(str(value))
...
self.id = (value1, value2)
(您没有发布什么sha512
。我假设您正在使用hashlib.sha512
, 并且sha512
正在调用该update
方法,该方法需要一个字符串作为输入。)
我对这里的编码实践有一些疑问。作为 API 的用户,我总是假设我设置为属性的任何内容都可以从同一个属性中取回。除此之外,拥有一个叫做id
mutable 的东西看起来很可疑。
至于传递两个值,怎么样:
@id.setter
def id(self, vals):
value1, value2 = vals
self._id = sha512(value1+value2)
然后分配一个元组:
myobj.id = (val1, val2)
我不知道这是否是您想要的,但这是传递两个或更多值的另一种方法:
@idx.setter
def idx(self, args):
self._idx = sha512(''.join([str(i) for i in args]))
您可以避免 list 或 tuple 的限制:
a.idx = 'zere', 'fezfz'
a.idx = ("eddez", "erez")
a.idx = 'deze', 'drezre', 'ezre'
您必须在 init 中测试异常并处理异常。
您也可以像这样使用property()函数。
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())