构造函数中没有默认参数,所有默认参数都在2D Lis中

2024-06-16 10:39:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我当前的代码:

wormmdscatch.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中的第三周,我还在学习


Tags: pyselfinitdefbluehifilesay
1条回答
网友
1楼 · 发布于 2024-06-16 10:39:17

在调用该函数之前,可以将2d数组拆分为3个列表,也可以不首先加入它们。然后,在函数内部,如果需要将它们连接在一起,您可以轻松地将它们再次连接在一起,从而允许您使用默认值设置其中的两个,并保留一个作为要求

相关问题 更多 >