【问题标题】:How to call class method with self in python, which I need to transfer values? [duplicate]如何在python中用self调用类方法,我需要传递值? [复制]
【发布时间】:2020-03-19 06:14:46
【问题描述】:

我在我的 IDE 中用 python 做 leetcode 70。但是它出现了错误。

class Solution:
    def climbStairs(self, n: int) -> int:
        cache = [None] * (n + 1)
        return self._helper(n, cache)

    def _helper(self, n, cache):
        if n < 0:
            return 0
        if n == 1 or n == 0:
            return 1
        if cache[n]:
            return cache[n]
        cache[n] = self._helper(n - 1, cache) + self._helper(n - 2, cache)
        return cache[n]

step = Solution.climbStairs(13)

print(step)

错误是:

`Traceback(最近一次调用最后一次): 文件“/home/viktor/PycharmProjects/70. Climbing Stairs/70. Climbing Stairs.py”,第 42 行,在 step = Solution.climbStairs(13) 类型错误:climbStairs() 缺少 1 个必需的位置参数:'n'

我该如何处理?
谢谢

【问题讨论】:

    标签: python class methods call self


    【解决方案1】:

    你需要创建一个Solution 的实例并调用它的方法。

    s = Solution()
    step = s.climbStairs(13)
    

    否则没有self对象。

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 1970-01-01
      • 2015-08-09
      • 2020-01-02
      • 2021-12-13
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      相关资源
      最近更新 更多