Python 类型错误:缺少一个必需的位置参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50594441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:33:12  来源:igfitidea点击:

TypeError: Missing one required positional argument

pythonobjecttypeerror

提问by Owen Martin

I am working on a game as a side project for fun and I have run into this error and I really don't know why it is happening...

我正在开发一个游戏作为一个有趣的副项目,我遇到了这个错误,我真的不知道为什么会发生这种情况......

Here is the code:

这是代码:

class players:
    def __init__(self, location, image_file, direction):
        self.location = location
        self.image_file = image_file
        self.direction = direction
        self.rect = self.image_file.get_rect()

    def turn(self, direction, playerImages):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] == True:
            self.direction -= 1
            if self.direction < -3:
                self.direction = 3
        if keys[pygame.K_d] == True:
            self.direction = 1
            if self.direction > 3:
                self.direction = 3

        if self.direction == -3:
            self.image_file = playerImages[0]
        if self.direction == -2:
            self.image_file = playerImages[1]
        if self.direction == -1:
            self.image_file = playerImages[2]
        if self.direction == 0:
            self.image_file = playerImages[3]
        if self.direction == 1:
            self.image_file = playerImages[4]
        if self.direction == 2:
            self.image_file = playerImages[5]
        if self.direction == 3:
            self.image_file = playerImages[6]

        return self.direction, self.image_file

I call it like:

我称之为:

skierDirection, playerImage = players.turn(skierDirection, playerImages)

The error I get is:

我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\Owen\Desktop\coding compile file\SkiFreeX\SkiFreeX.py", line 129, in <module>
    main()
  File "C:\Users\Owen\Desktop\coding compile file\SkiFreeX\SkiFreeX.py", line 122, in main
    skierDirection, playerImage = players.turn(skierDirection, playerImages)
TypeError: turn() missing 1 required positional argument: 'playerImages'
[Finished in 0.385s]

Any ideas?

有任何想法吗?

采纳答案by Mike Scotty

You are not supposed to call a class method directly, instead create an instance of that class:

您不应该直接调用类方法,而是创建该类的实例:

p1 = players(your, values, here)
skierDirection, playerImage = p1.turn(skierDirection, playerImages)

To elaborate on the error you're getting:

要详细说明您遇到的错误:

TypeError: turn() missing 1 required positional argument: 'playerImages'

类型错误:turn() 缺少 1 个必需的位置参数:'playerImages'

It's because turnneeds an instance of playersas first argument (self). A class method always gets passed the instance as the first argument, thus p1.turn(skierDirection, playerImages)will acutually pass 3 parameters to players.turn.

这是因为turn需要一个players作为第一个参数 ( self)的实例。类方法总是将实例作为第一个参数p1.turn(skierDirection, playerImages)传递,因此实际上将 3 个参数传递给players.turn.

回答by sandeep shewale

You have need to use () with class name

您需要将 () 与类名一起使用

Follow below code

按照下面的代码

skierDirection, playerImage = players().turn(skierDirection, playerImages)