简体   繁体   English

Class 中的 Function 错误:TypeError:函数()缺少 1 个必需的位置参数:

[英]Function in Class error: TypeError: function() missing 1 required positional argument:

I'm relatively new in programming with Python.我在使用 Python 编程方面相对较新。 This code was working perfectly, until I tried turning it into a class.这段代码运行良好,直到我尝试将它变成 class。 I'm making a class for my sudoku solver code, to practice classes and dabbling my toes in object oriented programming.我正在为我的数独求解器代码制作 class,以练习课程并在 object 面向编程中涉足我的脚趾。

So I have read a whole bunch of questions from users who have a similar problem, most answers were: -instantiate class first before calling a function from said class but none of them seem to work for my specific example.因此,我从有类似问题的用户那里阅读了一大堆问题,大多数答案是:-先实例化 class,然后再从所述 class 调用 function。

Here is my class:这是我的 class:

#assume sudoku is imported as a np.array, white spaces replaced by zeros


class Sudoku():

    solution_number = 1

    def __init__ (self, sud_arr):
        self.sudo = sud_arr

    #print(self.sudo)

    def possible (self, y, x, num):
        for i in range(9):
            if self.sudo[y][i] == num:
                return False
            if self.sudo[i][x] == num:
                return False
            yy = (y//3)*3
            xx = (x//3)*3
            for i in range(3):
                for j in range(3):
                    if self.sudo[yy+i][xx+j] == num:
                        return False
        return True


    def solve(self):
        for i in range(9):
            for j in range(9):
                if self.sudo[i][j] == 0:
                    for nr in range(1,10):
                         if Sudoku.possible(i,j,nr): #line 34
                            self.sudo[i][j] = nr
                            Sudoku.solve()
                            self.sudo[i][j] = 0
                    return
        if Sudoku.solution_number > 1:  #if there is more than one solution, include solution number
            print("Solution Number {}".format(Sudoku.solution_number))
        else: print("Solution Number 1")
        print(self.sudo)                                  
        Sudoku.add_sol_num()

    @classmethod
    def add_sol_num(cls):           
        cls.solution_number += 1

After running:运行后:

s = Sudoku(su) #where su is a numpy array sudoku
s.solve() #line 52

I get the error:我得到错误:

  File "/Users/georgesvanheerden/Python/Projects/Sudoku/SudokuSolver.py", line 52, in <module>
    s.solve()
  File "/Users/georgesvanheerden/Python/Projects/Sudoku/SudokuSolver.py", line 34, in solve
    if Sudoku.possible(i,j,nr):
TypeError: possible() missing 1 required positional argument: 'num'
[Finished in 1.9s with exit code 1]

Sorry if this is too much code, I didn't know which parts to cut out.抱歉,如果代码太多,我不知道要删掉哪些部分。

use self.possible when using a method, Sudoku.possible gets you a reference to that method that cant find the instance that you are calling it from.在使用方法时使用self.possible ,Sudoku.possible 会为您提供对该方法的引用,该方法无法找到您从中调用它的实例。

That also applies to if Sudoku.solution_number > 1 , generally the pythonic way is to use the self variable, or the first argument to the method (although you can also pass self to the function: Solution.possible(self, i, j, nr) )这也适用于if Sudoku.solution_number > 1 ,通常pythonic方法是使用self变量或方法的第一个参数(尽管您也可以将self传递给 function: Solution.possible(self, i, j, nr)

So your code would look like:所以你的代码看起来像:

    def solve(self):
        for i in range(9):
            for j in range(9):
                if self.sudo[i][j] == 0:
                    for nr in range(1,10):
                         if self.possible(i,j,nr): #line 34
                            self.sudo[i][j] = nr
                            self.solve()
                            self.sudo[i][j] = 0
                    return
        if self.solution_number > 1:  #if there is more than one solution, include solution number
            print("Solution Number {}".format(self.solution_number))
        else: print("Solution Number 1")
        print(self.sudo)                                  
        Sudoku.add_sol_num() # add_sol_num is a @classmethod

you can add self as the first argument:您可以添加 self 作为第一个参数:

if Sudoku.possible(self, i, j, nr):  #line 34

Let us first understand the error that is being given:让我们首先了解给出的错误:

TypeError: possible() missing 1 required positional argument: 'num'

This says that there is no value for num argument while calling possible() method.这表示在调用possible()方法时num参数没有值。

But you are passing 3 arguments here:但是您在这里传递了 3 arguments:

if Sudoku.possible(i,j,nr)

So what went wrong here???那么这里出了什么问题???

If you see the definition of your method:如果您看到方法的定义:

def possible (self, y, x, num):

This says that you will be passing 4 arguments and one of which would be the instance/object of the class ( self argument).这表示您将传递 4 个 arguments ,其中一个将是 class 的实例/对象( self参数)。

1. If we invoke this method using a class object, `self` argument is passed by default. So in this case we can just send 3 arguments apart from `self`.

2. If you want to invoke this method like you have done above, you will have to provide a value for  `self` argument explicitally.

So, here is how you can do it (pythonic way and good approach): While invoking the method possible, use self keyword.因此,您可以这样做(pythonic 方式和好方法):在调用可能的方法时,使用self关键字。

if self.possible(i,j,nr):

In lines 34 and 36 you call two methods as if they are static methods since you call the methods on the class not on some instance.在第 34 行和第 36 行中,您调用了两个方法,就好像它们是 static 方法一样,因为您在 class 上调用方法而不是在某些实例上。 That also is the reason, why self is not recognized and hence asked for another parameter in the method call.这也是为什么self未被识别并因此在方法调用中要求另一个参数的原因。 You want to call the methods of the current instance of Sudoku.你想调用当前数独实例的方法。 Therefore所以

if self.possible(i,j,nr):

in line 34 and在第 34 行和

self.solve()

in line 36 should do the trick.在第 36 行应该可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:get() 缺少 1 个必需的位置参数:function 中的“url”错误 - TypeError: get() missing 1 required positional argument: 'url' error in function SOLVED 函数缺少1个必需的位置参数 - Function missing 1 required positional argument 类型错误:FUNCTION NAME () 缺少 1 个必需的位置参数:'v_air - TypeError: FUNCTION NAME () missing 1 required positional argument: 'v_air Python 3异常:TypeError:函数缺少1个必需的位置参数:&#39;words&#39; - Python 3 Exception: TypeError: function missing 1 required positional argument: 'words' TypeError:函数缺少1个必需的位置参数:“ path”烧瓶Python - TypeError: function missing 1 required positional argument: 'path' Flask Python 调用 function 导致 TypeError: missing 1 required positional argument: 'self' - Calling function causes TypeError: missing 1 required positional argument: 'self' 类型错误:“函数”缺少一个必需的位置参数:“自我” - TypeError: 'Function' missing one required positional argument: 'self' 从不同的类调用函数会给出TypeError:缺少1个必需的位置参数 - Calling function from different class gives TypeError: missing 1 required positional argument python Tkinter 调用 function 在 class 之间调用 function - python Tkinter call function between class gets TypeError __init__() missing 1 required positional argument: 'parent' 调用 python function 时出错,类型错误:returnbook() 缺少 1 个必需的位置参数:'self' - Error while calling python function, TypeError: returnbook() missing 1 required positional argument: 'self'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM