【发布时间】:2019-04-17 09:06:54
【问题描述】:
在使用 redis 排序集时,我想让类方法更具可读性。在 redis-py 中,尤其是在 sorted set 中,push 和 update 操作相同。例如,
class A(object):
def push(self, key, value, score):
return redis.zadd(key, {value: score})
def update(self, key, value, score):
return self.push(key, value, score)
if __name__ == 'main':
a = A()
# push item1 in redis sorted set
a.push('sorted_set', 'item1', 1)
# update item1 in redis sorted set to score 2
# but I also know that this is same with
# a.push('sorted_set', 'item1', 2)
a.update('sorted_set', 'item1', 2)
但是,我想知道有没有更好的方法来解决这个问题。请告诉我。
【问题讨论】:
-
停止使用 python 2.7 它将被弃用
-
@andreihondrari,如果我使用 python 3.x 那么解决这个问题有什么好处?
-
避免将来出现问题