两个类方法在功能上相同,但名称不同



当使用redis排序集时,我想使类方法更具可读性。在 redis-py 中,特别是在排序集中,pushupdate操作的工作方式相同。例如

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

另请参阅此。

最新更新