Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use lambda as method within a class?

Tags:

python

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?

like image 591
Anshul Goyal Avatar asked Dec 02 '14 07:12

Anshul Goyal


People also ask

How do you use lambda in python class?

Syntax. Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).

How do you call a lambda function in Java?

You can invoke a Lambda function by creating an AWSLambda object and invoking its invoke method. Create an InvokeRequest object to specify additional information such as the function name and the payload to pass to the Lambda function.

Which method is use lambda?

You can use lambda function in map() map() function applies a given function to all the itmes in a list and returns the result. Similar to filter() , simply pass the lambda function and the list (or any iterable, like tuple) as arguments.

Can you pass a lambda function in python?

Using lambdas with Python built-ins Lambda functions provide an elegant and powerful way to perform operations using built-in methods in Python. It is possible because lambdas can be invoked immediately and passed as an argument to these functions.


2 Answers

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"))    
like image 170
Marcin Avatar answered Oct 12 '22 01:10

Marcin


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

like image 43
ThinkChaos Avatar answered Oct 12 '22 01:10

ThinkChaos