0

I'd like to make a function behave in a certain way if it's called by a specific function.

For example:

def addition(a, b):
   math_operations(a, b)
  
def math_operations(a, b):
   if this function is called by addition(a, b):
      return a + b
   else:
        return a * b

Is something like this even possible?

3
  • You can pass in a function as an argument to another function Commented Oct 2, 2023 at 16:53
  • @Seb_ Does this answer your question? stackoverflow.com/questions/2654113/… Commented Oct 2, 2023 at 16:57
  • @BijoThomas yes, I think that will do, thank you. Commented Oct 2, 2023 at 17:01

1 Answer 1

0

You can pass in a function as an argument and use that condition as part of your if statement.

Minimal example would be something like this:

def fun1(a,b):
    fun2(a,b,fun1)
    
def fun2(a,b, another_func):
    if another_func == fun1:
        print('does something')
fun1('a', 'b')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.