anonymous-function

creating a dictionary with anonymous functions in Python

creating a dictionary with anonymous functions in Python Question: I have a vector with some parameters and I would like to create a dictionary of anonymous (lambda) functions in Python3. The goal of this is to create a callable function that gives values equal to the sum of these functions with the same argument. I …

Total answers: 1

Lambda in a loop

Lambda in a loop Question: Considering the following code snippet: # directorys == {‘login’: <object at …>, ‘home’: <object at …>} for d in directorys: self.command["cd " + d] = (lambda : self.root.change_directory(d)) I expect to create a dictionary of two function as following : # Expected : self.command == { "cd login": lambda: self.root.change_directory("login"), …

Total answers: 4

Python: pass statement in lambda form

Python: pass statement in lambda form Question: A Python newbie question, why is this syntax invalid: lambda: pass, while this: def f(): pass is correct? Thanks for your insight. Asked By: Rez || Source Answers: That is an error because after the colon you have to put the return value, so: lambda: pass is equal …

Total answers: 4

Choose Python function to call based on a regex

Choose Python function to call based on a regex Question: Is it possible to put a function in a data structure, without first giving it a name with def? # This is the behaviour I want. Prints “hi”. def myprint(msg): print msg f_list = [ myprint ] f_list[0](‘hi’) # The word “myprint” is never used …

Total answers: 14

How to sort with lambda in Python

How to sort with lambda in Python Question: I am trying to sort some values by attribute, like so: a = sorted(a, lambda x: x.modified, reverse=True) I get this error message: <lambda>() takes exactly 1 argument (2 given) Why? How do I fix it? This question was originally written for Python 2.x. In 3.x, the …

Total answers: 5