【问题标题】:Two class methods were same functionally but got an different name两个类方法在功能上相同,但名称不同
【发布时间】:2019-04-17 09:06:54
【问题描述】:

在使用 redis 排序集时,我想让类方法更具可读性。在 redis-py 中,尤其是在 sorted set 中,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)

但是,我想知道有没有更好的方法来解决这个问题。请告诉我。

【问题讨论】:

  • 停止使用 python 2.7 它将被弃用
  • @andreihondrari,如果我使用 python 3.x 那么解决这个问题有什么好处?
  • 避免将来出现问题

标签: python redis redis-py


【解决方案1】:

我从未见过这种用法,所以这可能不是“推荐”的,但从技术上讲,你可以这样做。

class A(object):
    def push(self, key, value, score):
        return redis.zadd(key, {value: score})

    update = push

另见this

【讨论】:

  • 感谢分享。作为关键字,我第一次遇到了函数重命名和向后可比性。
猜你喜欢
  • 2013-01-21
  • 2022-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
相关资源
最近更新 更多