【问题标题】:Simple but too many positional arguments for method call方法调用的简单但位置参数太多
【发布时间】:2019-08-24 20:07:48
【问题描述】:
  1. 谁能解释为什么TypeError

    fib() 接受 1 个位置参数,但给出了 2 个`

    当我只给它一个参数时 => self.fib(self.n - 1)

  2. 另外,在保持类的实例化的同时,您能否提出解决此问题的解决方案?

  3. 我觉得“自我”被用得太多了,尤其是我添加的方法越多。可以改进吗?

只是想了解一些基础知识!感谢所有反馈。

class math_func:
    def __init__(self, n: "int" = 6, output: "list" = []):
        self.n = n
        self.output = output

    def fib(self):
        print("Current output is:", self.output)
        if self.n == 0:
            return self.output
        else:
            if len(self.output) < 2:
                self.output.append(1)
                self.fib(self.n - 1)
            else:
                last = self.output[-1]
                second_last = self.output[-2]
                self.output.append(last + second_last)
                self.fib(self.n - 1)
            return self.output

first_func = math_func(n=9)
print(first_func.fib())

【问题讨论】:

    标签: python class methods self


    【解决方案1】:

    方法的第一个参数是self,它指的是正在调用该方法的对象。如果您希望传递一个参数(看起来像n),则需要将其包含在方法的签名中。

    【讨论】:

      【解决方案2】:

      此函数接受一个参数self,但这一行:self.fib(self.n - 1) 尝试传递 2:selfself.n - 1

      【讨论】:

        【解决方案3】:

        当你第一次调用 fib() 时

        打印(first_func.fib())

        除了隐含的 self 之外,您不传递任何参数。在下一次调用中,您传递 1 arg 使其成为 2(包括隐式 self)

        self.fib(self.n - 1)

        这提供了 TypeError:fib() 接受 1 个位置参数,但给出了 2 个

        关于斐波那契,一个更简单的实现:

        def fib(n):
            if n==0 or n==1:
                return 1
            else:
                return (fib(n-1) + fib(n-2))
        

        【讨论】:

        • 谢谢。是的,那是正确的,但在这一个上,我玩的是动态编程而不是递归。另外,通过实例化...
        【解决方案4】:

        类型错误可能意味着很多事情。在这种情况下,这意味着您向函数传递了太多参数。看起来您正在使用参数调用 self.fib。您仅使用 self 作为参数对其进行了定义。从您编写代码的方式来看,您似乎认为需要将 self 作为参数传递。事实上,它是一个不可见的参数,在你调用函数时已经传递了。

        做这样的事情:

        class math_func:
                def __init__(self, '''...'''):
                    #...
                def fib(self):
                    #...
                    self.n = self.n - 1
                    self.fib()
        

        class math_func:
                def __init__(self, '''...'''):
                    #...
                def fib(self):
                    #...
                    self.fib2(self.n -1)
                def fib2(self, n: int):
                    #...
                    #same thing as fib with slight changes
        

        class math_func:
            def __init__(self, '''...'''):
                #...
            def fib(self, n: int):
                #...
                self.fib(self.n - 1)
            def getN(self):
                return self.n
        
        fib = math_func('''...''')
        print(fib.fib(fib.getN())
        

        【讨论】:

          【解决方案5】:

          你需要包含 n 作为 fib 的 agument

          def fib(self, n):
          ...
          

          还有一点补充:如果你定义递归函数,你应该考虑使用 lru_cache 装饰器来提高性能。

          from functools import lru_cache
          
          ...
          
          @lru_cache(500) # 500 is the cache size
          def fib(self, n):
          ...
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-10
            • 1970-01-01
            • 2019-10-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-28
            相关资源
            最近更新 更多