👨‍💻 dev automata

development

fukurou

the supreme coder
ADMIN
Python:
def greet():
    print("Hello!")

def farewell():
    print("Goodbye!")

def add(a, b):
    print(a + b)

# Dictionary mapping int → function
actions = {
    1: greet,
    2: farewell,
    3: lambda: add(5, 7),   # using a lambda wrapper
}

# Using the dictionary
actions[1]()   # calls greet()
actions[2]()   # calls farewell()
actions[3]()   # calls add(5, 7)
 

fukurou

the supreme coder
ADMIN
Python:
class Calculator:
    def add(self, a, b):
        return a + b
    
    def sub(self, a, b):
        return a - b

calc = Calculator()

operations = {
    1: calc.add,
    2: calc.sub,
}

print(operations[1](10, 3))  # 13
print(operations[2](10, 3))  # 7
 

fukurou

the supreme coder
ADMIN
Python:
from functools import partial

class Calculator:
    def add(self, a, b):
        return a + b
    
    def sub(self, a, b):
        return a - b

calc = Calculator()

operations = {
    1: partial(calc.add, 10, 3),   # add(10, 3)
    2: partial(calc.sub, 10, 3),   # sub(10, 3)
}

print(operations[1]())  # 13
print(operations[2]())  # 7
 
Top