Python 类函数中的可选参数

Python 类函数中的可选参数,python,python-2.7,Python,Python 2.7,我正在尝试使用python在pygame中制作一个2048游戏。如果没有给出参数,我希望函数stackright和stackleft对self.data进行操作,如果参数中给出了矩阵,则返回stackrighted或stacklefted矩阵。我将如何执行此操作。我正在使用python 2.7 这是我的密码: import random def getnull(): # returns null matrix data = [ [0,0,2,2], [0,4,0,2],

我正在尝试使用python在pygame中制作一个2048游戏。如果没有给出参数,我希望函数stackright和stackleft对self.data进行操作,如果参数中给出了矩阵,则返回stackrighted或stacklefted矩阵。我将如何执行此操作。我正在使用python 2.7 这是我的密码:

import random
def getnull(): # returns null matrix
    data = [
    [0,0,2,2],
    [0,4,0,2],
    [0,2,0,0],
    [2,0,2,0]
    ]
    return data

class Data:
    def fillrandom(self): #updates data by adding 2/4 randomly
        exit = False
        while not exit:
            y = random.randint(0,3)
            x = random.randint(0,3)
            if self.data[y][x] == 0:
                if random.randint(1,10) == 1:
                    self.data[y][x] = 4
                    exit = True
                else:
                    self.data[y][x] = 2
                    exit = True
    def alignright(self):   #
        list1 = [[],[],[],[]]
        for i in range(4):          # per row loop
            for count in range(4):   # per column loop
                if self.data[i][count] != 0:
                    list1[i] += [self.data[i][count]]
            list1[i] = [0]*(4-len(list1[i])) + list1[i]
        self.data = list1
    def alignleft(self):
        list1 = [[],[],[],[]]
        for i in range(4):          # per row loop
            for count in range(4):   # per column loop
                if self.data[i][count] != 0:
                    list1[i] += [self.data[i][count]]
            list1[i] = list1[i] + [0]*(4-len(list1[i]))
        self.data = list1
    def printstate(self):
        print(self.data[0])
        print(self.data[1])
        print(self.data[2])
        print(self.data[3])
        print("-------")
    def checkfilled(self):
        for count in range(4):
            for i in range(4):
                if self.data[count][i] == 0:
                    return False
        return True
    def stackright(self):
        for i in range(4):
            if self.data[i][3] == self.data[i][2]:
                if self.data[i][1] == self.data[i][0]:
                    self.data[i][3] = self.data[i][3] *2
                    self.score += self.data[i][3]
                    self.data[i][2] = self.data[i][1] *2
                    self.score += self.data[i][2]
                    self.data[i][0] , self.data[i][1] = 0,0
                else:
                    self.data[i][3] = self.data[i][3] *2
                    self.score += self.data[i][3]
                    self.data[i][2] = self.data[i][1]
                    self.data[i][1] = self.data[i][0]
                    self.data[i][0] = 0
            elif self.data[i][2] == self.data[i][1]:
                    self.data[i][2] = self.data[i][2] *2
                    self.score += self.data[i][2]
                    self.data[i][1] = self.data[i][0]
                    self.data[i][0] = 0
            elif self.data[i][1] == self.data[i][0]:
                self.data[i][1] = self.data[i][1] *2
                self.score += self.data[i][1]
                self.data[i][0] = 0
    def stackleft(self):
        for i in range(4):
            if self.data[i][0] == self.data[i][1]:
                if self.data[i][2] == self.data[i][3]:
                    self.data[i][0] = self.data[i][0] *2
                    self.score += self.data[i][0]
                    self.data[i][1] = self.data[i][2] *2
                    self.score += self.data[i][1]
                    self.data[i][3] , self.data[i][2] = 0,0
                else:
                    self.data[i][0] = self.data[i][0]*2
                    self.score += self.data[i][0]
                    self.data[i][1] = self.data[i][2]
                    self.data[i][2] = self.data[i][3]
                    self.data[i][3] = 0
            elif self.data[i][1] == self.data[i][2]:
                    self.data[i][1] = self.data[i][1] *2
                    self.score += self.data[i][1]
                    self.data[i][2] = self.data[i][3]
                    self.data[i][3] = 0
            elif self.data[i][2] == self.data[i][3]:
                self.data[i][2] = self.data[i][2] *2
                self.score += self.data[i][2]
                self.data[i][3] = 0
    def alignup(self):
        col = [[],[],[],[]]
        for i in range(4): #per col loop
            for count in range(4): #per row loop
                if self.data[count][i] != 0:
                    col[i] += [self.data[count][i]]
            col[i] = col[i] + [0]*(4-len(col[i]))
        for i in range(4):      # convert column to row
            for count in range(4):
                self.data[count][i] = col[i][count]
    def aligndown(self):
        col = [[],[],[],[]]
        for i in range(4): #per col loop
            for count in range(4): #per row loop
                if self.data[count][i] != 0:
                    col[i] += [self.data[count][i]]
            col[i] = [0]*(4-len(col[i])) + col[i]
        for i in range(4):       # convert column to row
            for count in range(4):
                self.data[count][i] = col[i][count]
    def __init__(self):
        self.data = getnull()
        self.score = 0
data = Data()
data.aligndown()
data.printstate()
print(data.score)



while True:
    pass

您可以使用默认参数。例如:

def func(self, matrix=None):
    if matrix is None:
        #do stuff with self.data
    else:
        #do stuff
这样,如果未给出任何参数,则默认值为None

因此,您知道如果
矩阵的值为None,则调用方没有指定值,因此您可以使用
self.data
执行操作。但是如果他确实指定了一个值(
else
),那么这意味着调用者指定了一个值,您可以对矩阵做些什么

或者,如果要将它们用作相同的值,可以执行以下操作:

def func(self, matrix=None):
    if matrix is None: matrix = self.data
    #do stuff with the variable 'data'

现在,
数据
是你想要的任何东西

像这样的东西可以达到目的:

def stackright(self, *args):
    if len(args) == 0:
        #do things with no arguments
    else:
        #do things with arguments
        print(args[0], args[1], ...)
如果希望能够按参数名称调用参数,也可以将
*args
替换为
**kwargs
。您甚至可以同时使用这两种方法:

def f(self, madatoryArgument1, mandatoryArgument2, *args, **kwargs)

与使用
None
默认值传递参数相比,这种方法的优点是,当参数数量增加时,它可以简化参数传递过程。

对于您所要求的,您的函数如下所示:

def stackright(self, data=None):
    if data is None: data = self.data
    for i in range(4):
        if data[i][3] == data[i][2]:
            if data[i][1] == data[i][0]:
                data[i][3] = data[i][3] *2
                self.score += data[i][3]
                data[i][2] = data[i][1] *2
                self.score += data[i][2]
                data[i][0] , data[i][1] = 0,0
            else:
                data[i][3] = data[i][3] *2
                self.score += data[i][3]
                data[i][2] = data[i][1]
                data[i][1] = data[i][0]
                data[i][0] = 0
        elif data[i][2] == data[i][1]:
                data[i][2] = data[i][2] *2
                self.score += data[i][2]
                data[i][1] = data[i][0]
                data[i][0] = 0
        elif data[i][1] == data[i][0]:
            data[i][1] = data[i][1] *2
            self.score += data[i][1]
            data[i][0] = 0
    return data
def stackleft(self, data=None):
    if data == None: data = self.data
    for i in range(4):
        if data[i][0] == data[i][1]:
            if data[i][2] == data[i][3]:
                data[i][0] = data[i][0] *2
                self.score += data[i][0]
                data[i][1] = data[i][2] *2
                self.score += data[i][1]
                data[i][3] , data[i][2] = 0,0
            else:
                data[i][0] = data[i][0]*2
                self.score += data[i][0]
                data[i][1] = data[i][2]
                data[i][2] = data[i][3]
                data[i][3] = 0
        elif data[i][1] == data[i][2]:
                data[i][1] = data[i][1] *2
                self.score += data[i][1]
                data[i][2] = data[i][3]
                data[i][3] = 0
        elif data[i][2] == data[i][3]:
            data[i][2] = data[i][2] *2
            self.score += data[i][2]
            data[i][3] = 0
    return data
然后调用data.stackright()将对自身数据进行操作,但调用data.stackright(matrix2)将对matrix2进行操作(将分数添加到数据时,除非您更改上述部分)