Sure.

It's a trick I saw used elsewhere, which makes for much more reusable code (and which I should've used back when someone was having a lot of trouble editing an attached python script

).
Also, noticing your edits, you seemed to be slightly confused at first what GPM is.
It isn't python, it is a cut-down mini-language that compiles to python and is documented in the
calibre manual. You can however define new functions in python.