python默认参数语法错误 [英] python default argument syntax error
问题描述
我刚刚在 python 中为使用 pygame 编写的游戏编写了一个小文本类,但由于某种原因,我的默认参数不起作用.我试着查看 python 文档,看看这是否能让我知道我做错了什么,但我没有完全理解文档中的语言.似乎没有其他人遇到此问题.
I just wrote a small text class in python for a game written using pygame and for some reason my default arguments aren't working. I tried looking at the python documentation to see if that might give me an idea what I did wrong, but I didn't fully get the language in the documentation. No one else seems to be having this issue.
这是文本类的代码:
class Text:
def __init__(self,screen,x_location='center',y_location='center',writeable,color,size=12):
self.font = pygame.font.Font("Milleni Gem.ttf", size)
self.text = self.font.render(writeable,True,color)
self.textRect = self.text.get_rect()
if x_location == "center":
self.textRect.centerx = screen.get_rect().centerx
else:
self.textRect.x = x_location
if y_location == "center":
self.textRect.centery = screen.get_rect().centery
else:
self.textRect.y = y_location
self.update()
def update(self):
screen.blit(self.text,self.textRect)
这是调用它的代码:
from gui import *
Text(screen,75,75,currentweapon.clip)#currentweapon.clip is an integer
我得到的错误是这样的:
The error I get is this:
SyntaxError: non-default argument follows default argument
并指向代码中的 def __init__()
行.这个错误是什么意思,我在这里做错了什么?
and points to the def __init__()
line in the code. What does this error mean and what am I doing wrong here?
推荐答案
def __init__(self,screen,x_location='center',y_location='center',writeable,color,size=12):
您在默认参数之后定义了非默认参数,这是不允许的.你应该使用:
You have defined non-default arguments after default arguments, this is not allowed. You should use:
def __init__(self,screen,writeable,color,x_location='center',y_location='center',size=12):
这篇关于python默认参数语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!