【问题标题】:What is the benefit to reference a method in a class? [closed]在类中引用方法有什么好处? [关闭]
【发布时间】:2019-01-12 19:58:08
【问题描述】:

我在 GitHub 上找到了与我的 ExampleClass 类似的代码,但我不明白使用 getx = calcualte_x 的好处。我觉得它有点接近@property,但并不明显。

那条线有什么好处,为什么不直接使用class-instance.calcualte_x(add) 而不是class-instance.getx(add)

class ExampleClass():
    def __init__(self, x_val):
        self.x = x_val

    def calcualte_x(self, add):
        x = self.x + add

    getx = calcualte_x

【问题讨论】:

    标签: python python-3.x class methods


    【解决方案1】:

    它可以是别名——例如为了便于使用或向后兼容。

    【讨论】:

    • 你好本杰明。您能否在“向后兼容性”的情况下指定优势
    • 如果您的代码被其他人使用并且您不想破坏他们的代码,您必须在不更改 API 的情况下修改您的代码。有关向后兼容性的更多信息,您可以在此处阅读:Backward compatibility
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多