为什么我得到的语法无效

时间:2019-07-05 21:13:49

标签: python syntax-error

我正在尝试设置我的 init ,并且在尝试设置(x,y)时,在坐标的空心括号上收到了无效的语法错误。

我觉得这将是一个明显的错误,但是我已经看了太久了,可能会用一些新鲜的眼睛...

def __init__(self, (x,y), size, color = (255,255,255)):
        self.x = x
        self.y = y
        self.size = size
        self.color = color
        self.width = width

2 个答案:

答案 0 :(得分:3)

由于x和y是元组的元素(如果这是您要创建的元素),则无需在参数中分配单个元素。

def __init__(self, coordinates, size, color = (255,255,255)):
        self.x = coordinates[0]
        self.y = coordinates[1]
        self.size = size
        self.color = color
        self.width = width

答案 1 :(得分:0)

您必须编写__init__(self, v , size, color = (255,255,255))并将每个v作为列表传递。

def __init__(self, v, size, color = (255,255,255)):
        self.x = v[0] #first coordinate of v
        self.y = v[1] #second coordinate of v
        self.size = size
        self.color = color
        self.width = width
相关问题