How to use lambda as method within a class?
Use lambda self, x: "...%s..." % x
You have few possibilities of using/accessing class variables of lambdas. Three of them are:
class BaseClass(object):
successify = lambda x: "<Success>%s</Success>" % x
errorify = lambda x: "<Error>%s</Error>" % x
def try1(self):
print(self.__class__.successify("try1"))
def try2(self):
print(self.__class__.successify("try2"))
# or
class BaseClass(object):
successify = lambda x: "<Success>%s</Success>" % x
errorify = lambda x: "<Error>%s</Error>" % x
def try1(self):
print(BaseClass.successify("try1"))
def try2(self):
print(BaseClass.successify("try2"))
# or Please not changes to lambda definitions below
class BaseClass(object):
successify = lambda self,x: "<Success>%s</Success>" % x
errorify = lambda self,x: "<Error>%s</Error>" % x
def try1(self):
print(self.successify("try1"))
def try2(self):
print(self.successify("try2"))