构造函数中没有默认参数,所有默认参数都在二维列表中

时间:2019-02-12 19:17:22

标签: python

我当前的代码:

WormModScratch.py​​

class WormMod:
    def __init__(self,wormColor, appleOrangeBananaPos = [[-1,-1],[0,0],[3,4]]):
        self.currentWormColor = wormColor
        self.appleOrangeBananaPos = appleOrangeBananaPos

    def say_hi(self):
        print(self.appleOrangeBananaPos[0][0])
        print(self.appleOrangeBananaPos[0][1])
        print(self.appleOrangeBananaPos[1][0])
        print(self.appleOrangeBananaPos[2][0])
        print(self.appleOrangeBananaPos[2][1])
        # print(self.orangePos[0], self.orangePos[1])

    def getHighScore(self):
        highscore_file = open("highscore.txt", "r")
        scorelist = highscore_file.readlines()
        highscore_file.close()
        return scorelist

test2.py

from WormModScratch import WormMod

z = WormMod("blue")
z.say_hi()

print(z.getHighScore())

输出

-1
-1
0
3
4
['100\n', '50\n', '25\n']

------------------
(program exited with code: 0)
Press return to continue

我的问题:

def __init__(self,wormColor, appleOrangeBananaPos = [[-1,-1],[0,0],[3,4]]):

z = WormMod("blue")

我想让2dList中的第一个元素默认为 [2],[0,0],[3,4]我想这样做,因此WormMod必须包含两个apple x y值,而其他两个水果默认为0,03,4

z = WormMod("blue",[applex,appley])

如何如上所述完成两个未默认值和四个默认值?这只是我在python的第三周,还在学习中。

1 个答案:

答案 0 :(得分:0)

在调用函数之前,您可以将2d数组拆分为3个列表,也可以永远不要将它们连接在一起。然后,在函数内部,如果需要将它们连接起来,则可以轻松地将它们全部重新组合在一起,从而使您可以将其中两个设置为默认值,并保留其中一个为必填项。

相关问题