Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in Python to write lambda like objects, like in JS

In JS I can define an Object like this:

foo = {"foo": 42, "bar": function(){/* do something */}}

Is there a way to do the same thing in Python ?

like image 420
Marius Illmann Avatar asked Dec 18 '22 17:12

Marius Illmann


1 Answers

The following might work:

# Define a function    
def foo():
    print("foo")

# Define a dictionary    
d = {"a":1, "b":foo}

d["b"]()

Output: foo

like image 169
Employee Avatar answered Apr 30 '23 10:04

Employee