Checking if an optional paramater is specified in a function inside another function

Viewed 44

I have a function which has an optional parameter. Call this function func1 with optional argument a which is initially set to None

I want to use this function as an argument to another function call it func2.

Hence, I want a function like:

def func2(func1):
     If a is not None:
          .......

How would I achieve this?

3 Answers

If you are using func1's handler as an argument (which doesn't have arguments with parenthesis), you can't do this and need to define a as an argument for func2. Like this:

def func2(func1, a=None):
   ...

If you are using func1's return value, the argument's to func1 are lost when func2 is invoked and again the only solution I can think of is to define a as an argument for func2.

You can't check what parameter is passed to func1 using this -

def func2(func1):
     If a is not None:
          .......

In this, You are just passing the func1 to func2 as parameter. Nowhere has it been explicitly specified what parameter func1 will be using. So, You will have to decide what you are going to pass to func1 inside your func2 code.


Maybe you are looking for something like this -

def func2(func1,a=None):
     if a != None:       # When a non - None parameter is passed
          func1(a)
     else :
          # Something when a is none

Here, you can check what parameter is going to be passed to func1 since during the call to func2, you will have to specify either 1 or 2 arguments. a is set to None by default. If a 2nd parameter is not passed to func2, it means that no parameter for func1 has been given. This means func1 was meant to be called without a parameter ( or using the default set parameter value ).


Or maybe you are looking to do something like this -

def func1(a=None):
    func1.a = a
    print(a)
    
def func2(f):
     f(2)         # Called with a paramter, so f.a will not be None
     print(f.a)
     f()          # Called without a param, so f.a will be None
     print(f.a)

func2(func1)

Output :

2
2
None
None

Here, the function object will set the parameter passed to it as its object and then once it called, depending on what the value of func1.a is, you can get what was the value with which the function was called last time. You can modify the above snippet for what you asked in question as -

def func1(a=None):
    func1.a = a
    
def func2(f):
     if f.a is not None:
         print("Last call with paramter - " + str(f.a))
     else :
         print("Last Function call was made without a paramter")

func1()
func2(func1) 

func1(2)
func2(func1)

Output :

Last Function call was made without a paramter
Last call with paramter - 2

Hope this helps !

I'm not sure if this is the best way to do it, but to achieve what you desire, you can take advantage of the fact that functions (since they are objects) can be assigned with attributes.

def func(a=None):
    func.a = a
    print(a)

def func_2(f):
    if f.a is not None:
        print(f.a)

Note that in order for this to work as you want, you would need to make sure that func was called before func_2

>>> func(1)
1
>>> func_2(func)
1
Related