Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

python - pyProcedure method which adds a new mission

class Rocket:

    def __init__(self, name, mission):
        self.__name = name      
        self.__mission = mission

    def getMission(self): 
        return self.__mission
    def addMission(self, mission): 
        # procedure method which adds a new mission. There can be one (str) or multiple (list) existing missions

I can't understand how to implement this addMission func within a class correctly.

Output should be something like this: Missions: ['Dragon 2 pad abort test', 'Dragon 2 in-flight abort test']. What I've "come up with" simply produces the right answer if that default input is placed, albeit it has the wrong inner mechanics. Thus, with different input it won't produce a thing:

self.__mission = [self.__mission, mission]

How do I... make it work?

question from:https://stackoverflow.com/questions/65901140/pyprocedure-method-which-adds-a-new-mission

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

class Rocket:
    def __init__(self, name, mission):
        if not type(mission) in [list, tuple]:
            raise TypeError("constructor argument mission must be of type list or tuple")
        self.__name = name      
        self.__mission = mission

    def getMission(self): 
        return self.__mission
    def addMission(self, mission): 
        # procedure method which adds a new mission. There can be one (str) or multiple (list) existing missions
        if type(mission) == str:
            self.__mission.append(str)
        elif type(mission) in [list, tuple]:
                self.__mission.extend(miss)
        else:
            raise TypeError("addMission argument mission must have types str, list or tuple")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...