The issue you're running into is that self
is a local variable whithin each method. You can't make a class variable holding self.response.out.write
because self
is undefined at the top level of the class. I think there are two possible solutions:
The simplest is to just make a local alias in any method that is going to call self.response.out.write
a lot:
def i_write_a_lot(self):
alias = self.response.out.write
alias("stuff")
alias("more stuff")
alias("yet more stuff")
Another option would be to make the alias a property
object in the class, so it can be accessed anywhere in the class. This is much less obvious about where the alias comes from, which may cause confusion when somebody else (or you, months later) reads your code, but it comes closer to what you wanted:
@property
def alias(self):
return self.response.out.write
def method1(self):
self.alias("stuff")
def method2(self):
self.alias("other stuff")