Anshul Goyal
Anshul Goyal

Reputation: 77013

How to use lambda as method within a class?

Here is what I am trying to do

class BaseClass(object):
    successify = lambda x: "<Success>%s</Success>" % x
    errorify = lambda x: "<Error>%s</Error>" % x
    def try1(self):
        print successify("try1")
    def try2(self):
        print self.successify("try2")

But neither of the methods seem to work..

>>> BaseClass().try1()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in try1
NameError: global name 'successify' is not defined
>>> BaseClass().try2()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in try2
TypeError: <lambda>() takes exactly 1 argument (2 given)

How do I use lambdas as methods within a class?

Upvotes: 21

Views: 19890

Answers (4)

John Strood
John Strood

Reputation: 2029

Unfortunately, Marcin's first answer doesn't work in Python 2. To recap, he wrote

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"))

But this doesn't work in Python 2. An interesting trick would be to make the class believe it is a static method instead of a static variable. After changing the code suitably, it becomes:

class BaseClass(object):
    successify = staticmethod(lambda x: "<Success>%s</Success>" % x)
    errorify = staticmethod(lambda x: "<Error>%s</Error>" % x)
    def try1(self):
        print(self.__class__.successify("try1")) # Works!
    def try2(self):
        print(self.__class__.successify("try2")) # Works!

Upvotes: 0

Marcin
Marcin

Reputation: 238647

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"))    

Upvotes: 20

Neel
Neel

Reputation: 21307

If you want to access successify as object function then first argument is self means object instance.

you have to change your lambada function if you have to use self.successify

successify = lambda self, x: "<Success>%s</Success>" % x

Upvotes: 0

ThinkChaos
ThinkChaos

Reputation: 1853

Use lambda self, x: "...%s..." % x

Upvotes: 16

Related Questions