Discussion

Left ArrowBack to discussions page
jTRJjTRJ Posts: 3 Apprentice
edited November 2017 in Programming
I've been working on a Universal Robot a little now where I write my code in text format on my computer and then import it with the script command on the standard UR GUI. I don't like writing unorganized code, and I'd much prefer to put my functions into classes to keep it orderly and avoid interfering global variables. So far I have been unable to do it as I'd normally do on python, so I was hoping if anyone here had had any similar thoughts and/or successes?

Best Answer

  • matthewd92matthewd92 Posts: 693 Handy
    Accepted Answer
    There’s nothing that I’m aware of. I believe that if you define functions within a function that they are not accessible outside that function.   You can locally scope variables though within the function if that helps. 

Comments

  • jTRJjTRJ Posts: 3 Apprentice
    Sorry about that, I submitted it before finishing writing it.
  • jTRJjTRJ Posts: 3 Apprentice
    I was afraid so. The thing is that I want a group of functions to interact with some shared variables, but keeping it inaccessible to everything else. I have, for example, seperate functions for grabbing items and stacking them, and I initialize their parameters by asking the operator in two other functions. Now, if I could have a class for grabbing + grab initialization and a class for stacking + stack initialization, my code would be more flexible and organized when I later add more functions.

    Nevertheless, thank you for your quick response.
  • matthewd92matthewd92 Posts: 693Founding Pro, Tactile Sensor Beta Testers Handy
    I haven’t tested this but you could write different script files and I believe you can define at the top level local variables that are accessible to all functions in that script file but not the other script files. You’d have to test this theory though but if you can that would get you closer to accomplishing what you’re trying. Even more modular as you can import just the modules you need in the program. We do this now, but define most variables at least at the top scope as global so they appear in the variables list. We have broken up the script that we use into functional modules so we can use just what we need. 
Sign In or Register to comment.
Left ArrowBack to discussions page