0

I seem to be having a hard time grasping the concepts of argument passing and returning variables from a function. I am working on a project involving issuing commands to a robot through a python gui that I made using rest protocol. I am currently in the process of writing failsafe code to prohibit a user accidentally pressing the load mission button multiple times and loading a bunch of the same mission. Below is my code:

demo = 'e1af13ba-4f38-11e7-9626-f44d3061db09'
mission = 1


def compMissionQueue():
    getMissionQueue = robot.get_mission_queue()[1][0]
    #print(getMissionQueue)
    missionID = getMissionQueue['id']
    #print(missionID)
    missionGUID = robot.get_mission_GUID_from_queue(missionID)[1]['mission_id']
    print(missionGUID)
    if missionGUID != mission:
        print('Mission successfully added to queue')
    else:
        print('Mission already in queue. Add again?')

def techDemo():
    def loadMission():
        #runDemo = robot.load_mission(demo)
        #robot.continue_robot()
        #robot.pause_robot()
        #robot.delete_from_mission_queue(demo)
        mission = demo
        return mission
    loadMission()
    compMissionQueue()
    print(mission)
    return mission
    

The function techDemo is 'triggered' by a button press in the gui. What I was trying to do was to use the compMissionQueue function to check if a mission had already been loaded to the queue and if so, print some message to the user. I tried checking if the value for 'mission' is being returned by running multiple print calls on it and it is not returning mission=demo from what I can tell, it always prints '1' from the first time I set mission. If you could point me in the right direction, it would be much appreciated.

3
  • first of all your code does not run. It gives syntax error compMissionQueue() not defined Commented Jun 16, 2017 at 14:38
  • Well, you return a value from the nested loadMission function, but what do you do with that returned value? You need to assign it to something. Commented Jun 16, 2017 at 14:39
  • techDemo knows nothing about mission as it as a local variable in the scope of loadMission. You should do mission = loadMission() then yu will be able to return it from techDemo. Commented Jun 16, 2017 at 14:40

1 Answer 1

1

Your problem is you are referring to the variable 'mission' within different scopes.

Consider this example:

mission = 1
def set_mission():
    mission = 2
print(mission)
set_mission()
print(mission)

Will print:

1
1

'mission' didn't seem to get changed huh. What's happening is 'mission' is two separate variables, a global variable (defined line 1), and a local variable (defined line 3). The local instance exists only within the scope of set_mission(), and gets deleted afterwards. When we set 'mission' to 2, we set the local instance and not the global instance. When we go to print it out, we are printing the global instance, which never got touched!

There are two ways of dealing with this.

Option 1

Use only a global variable:

mission = 1
def set_mission():
    global mission
    mission = 2
print(mission)
set_mission()
print(mission)

or

Option 2

Pass the variable as an argument, then return it:

mission = 1
def set_mission(my_mission):
    my_mission = 2
    return my_mission
print(mission)
mission = set_mission(mission)
print(mission)

Both of these will return the desired

1
2

Be careful with scope, and if in doubt use different variable names within functions so you are less likely to get confused!

Sign up to request clarification or add additional context in comments.

3 Comments

I took your advice and used option 1 to use only the global variable for mission. So if I am understanding correctly, by adding the line "global mission" we are essentially telling the program that we are using the same "mission" variable within that function as we defined outside the scope of that function, correct? Also, Is it better or is there a conventional way that is preferred out of the two options you showed?
If there's no direct causal effect of changing the value of 'mission' it's perfectly acceptable to use option 1. By that I mean if after you set the variable it can sit there living a happy life until it needs to be read, then it's fine. However, if you want code to be executed whenever 'mission' is set, you should encapsulate it inside a setter function. For example, if whenever you set a new mission, you need to record the current time it was set, then you should use a setter.
Oh that makes sense! Thank you for the detailed explanation, helping me understand how scopes work, and when to use the different methods of dealing with them. Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.