global functions & classes

I have a bunch of Touch Bar buttons that do more or less the same thing but with varying parameters, 20 or so lines of near identical code. Is there a way I can declare global functions or classes - python, AppleScript, whatever - for these, make life easier for myself? It's in danger of all getting a bit messy and hard to control.