Python 两个类方法在功能上相同,但名称不同
在使用redis排序集时,我希望使类方法更具可读性。在redis py中,尤其是在排序集中,Python 两个类方法在功能上相同,但名称不同,python,redis,redis-py,Python,Redis,Redis Py,在使用redis排序集时,我希望使类方法更具可读性。在redis py中,尤其是在排序集中,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, val
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
另请参见。停止使用Python2.7,它将是deprecated@andreihondrari,如果我使用Python3.x,那么解决这个问题有什么好处呢?您避免了将来的问题谢谢分享。我第一次将函数重新命名和向后可比性作为关键字。