在使用redis sorted set时,我想让类方法更具可读性。在 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)
但是,我想知道是否有更好的方法来解决这个问题。请告诉我。
最佳答案
我从未见过这种用法,所以这可能不是“推荐”,但从技术上讲你可以这样做。
class A(object):
def push(self, key, value, score):
return redis.zadd(key, {value: score})
update = push
另见 this .
关于python - 两个类方法在功能上相同但名称不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55723851/