Vatsal Soni
Vatsal Soni

Reputation: 1

call a function with optional and name arguments in another function

How can we do function implementation for the following?

For optional and named arguments we write function in following way.

def funct(*args, **kwargs):
    for arg in args:
        print “arg =”, arg
    for key in kwargs.keys():
        print “arg[{0}]={1}”.format(key, kwargs[key])

If I want to call this function inside another function of same protocol, how can I implement?

def funct2(*args, **kwargs):
    # call funct() above
    funct(????????)

So is there any way to call the first funct() with optional arguments inside the new funct2() with the same type of optional arguments?

Upvotes: 0

Views: 50

Answers (1)

Peter Gibson
Peter Gibson

Reputation: 19574

The syntax for calling a function is basically the same as when defining the function. So your funct2 would look like:

def funct2(*args, **kwargs):
    # call funct() above
    funct(*args, **kwargs)

Here are the Python docs covering the subject: https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists

Upvotes: 1

Related Questions