问题
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.
Here's the code for the text class:
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)
and here's the code to call it:
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
and points to the def __init__() line in the code. What does this error mean and what am I doing wrong here?
回答1:
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):
来源:https://stackoverflow.com/questions/17893820/python-default-argument-syntax-error