【问题标题】:NameError is not defined in leetcode problemleetcode问题中没有定义NameError
【发布时间】:2021-11-09 23:59:04
【问题描述】:

请多多包涵,我还在习惯 Python 中的 OOP。准备面试,发现 Leetcode 题一般有如下结构。

class Solution:
   def solutionMethod(self, input):
      ...

我想创建一个对象来存储和更新我的解决方案时的数据。我的代码结构如下:

class Solution:

   def helperMethod(self, helperInput):
      self.var1 = helperInput[0]
      self.var2 = helperInput[1]
      self.var3 = self.var1 + self.var2

   def solutionMethod(self, input):
      
      currObject = helperMethod(input)
      # do stuff with currObject
      ...

我收到名称错误:未定义名称“helperMethod”。

我被甩了,因为我通常会在这里做错什么?我是否需要 init 才能调用 helperMethod?

在时间有限的编码面试中,开设这样的课程通常是个好主意吗?

关于 leetcode(和编码面试站)如何测试我的代码,我有什么需要注意的吗? 我想它运行如下,对吗?

soln = Solution()
soln.solutionMethod(input)

【问题讨论】:

  • currObject = self.helperMethod(input).

标签: python oop methods syntax


【解决方案1】:

你需要调用self对象上的方法:

class Solution:
   def solutionMethod(self, input):
      currObject = self.helperMethod(input)

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2020-09-20
    • 2020-01-17
    • 2022-06-12
    相关资源
    最近更新 更多