PSP provides a <psp:method> tag but that only lets you define a method using Python syntax. Sometimes you want to define a method using PSP syntax instead. If you're using Python 2.1 or higher, you can use nested scopes to accomplish this, in a somewhat hackish way. The best way to see how to do it is with an example:
<%@ page imports="__future__:nested_scopes" %> <%@ page imports="new:instancemethod" %> <%def writeFoo(self):%> Foo <%end%> <%self.writeFoo = instancemethod(writeFoo, self, self.__class__)%> <p>Before calling self.writeFoo() <p><%self.writeFoo()%> <p>After calling self.writeFoo()
Save this as a PSP file and try it out.
Note that this hack may be unsuitable for some applications because the method doesn't actually exist until the function definition is executed and the instance method created and assigned to self.
This technique can also be used to create normal functions (not methods) in PSP syntax -- just leave out the call to instancemethod() and the assignment to self and be sure not to call the function until after it is defined.
-- GeoffTalvola - 23 Apr 2002