I feel this is a very simple question but I can’t find to seem a proper answer. Basically I have a list of functions call from a class named simulation:
simulation.addGroup("teapotarmy")
simulation.populateGroup(20)
simulation.addNode("input",INPUT)
simulation.addNode("output",OUTPUT);
simulation.connectNodes("input","output");
simulation.manipOutputNode("output", "group.xvel");
simulation.manipInputNode("input", 1, 0.05);
Is there a way to call those functions without having to repeat the class name everytime? Something along the line of:
(thethingIwant) simulation:
addGroup("teapotarmy")
populateGroup(20)
addNode("input",INPUT)
...
I have done this in other programming languages but I haven’t figured out the syntax in Python. I have a faint memory of it having something to do with the ‘with’ statement…?
Thanks in advance.
Leon
Simply, no. There is no (good, see my comment at the end) way to do this. The best you could do is assign it to another, shorter name:
Which isn’t too bad, although I’d argue the normal method is the one that is more readable.
As an extra, it’s not strictly true that you can’t do this. You could assign all of the methods of simulation to the local namespace programatically, however, this would be pretty confusing to follow, and I’d advise against it.
Example:
Note this is pretty fragile – you would have to be careful and do stuff like checking you are not overwriting values, checking values haven’t been deleted before the context manager exits, etc… It’s also pretty unclear what is going on.
tl;dr: Yes, it’s possible, no, you shouldn’t do it. Your current code is fine and clear.