Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How can you execute functions with multiple arguments at a terminal?

I have a python function defined with multiple arguments as follows:

def print_funcargs(arg1, arg2, arg3):
      print arg1 + ' '+ arg2 + ' ' + arg3

How do I execute this function at a terminal?


1 Answer
Rajendra Dharmkar

We first import the sys module. We have to use the argv function of the sys module to fetch the arguments of the function entered at the terminal and execute the function.

#fubar.py
import sys
def print_funcargs(arg1, arg2, arg3):
      print arg1 + ' '+ arg2 + ' ' + arg3
if __name__ == "__main__":
      a = sys.argv[1]
      b = sys.argv[2]
      c = sys.argv[3]
print_funcargs(a,b,c)
print sys.argv

At the terminal if we write

$ python fubar.py  I adore books

OUTPUT

I adore books
['fubar.py', 'I', 'adore', 'books']
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.