I’m working on porting my main project from Python 2 to Python 3. For the most part things have gone reasonably well: Once I found out how to direct it to all of my Python files in a single command, “2to3” did the first pass at conversion. (And even seems to have saved backups!) Strings look like they’re more of a pain now, but there seem to be solutions that are simple enough.
However, I’ve hit a problem that I don’t know what to do about, and I don’t seem to be finding solutions online. (Although I may well have missed something.)
My problem is this:
In certain cases, I want to be able to save and restore references to methods–such as when saving and restoring a mapping from key-press to key-event.
Under Python 2 this was achieved by saving the string “instancemethod” as the “type” of the object being saved, and the method’s name as the “value” of the object. When loaded, the game-saving module would then, as per usual, employ “eval” to construct the desired object from its type-string and value–in this case ending up with “eval(‘instancemethod’+(methodName)”. This produced the desired method, and allowed me to restore such method-references.
However, under Python 3 I get the error “NameError: name ‘instancemethod’ is not defined”.
Does anyone here know how to achieve the same thing under Python 3 as I was under Python 2?