Bootstrap FreeKB - Python (Scripting) - Pass values into a function
Python (Scripting) - Pass values into a function

Updated:   |  Python (Scripting) articles

If you are not familiar with functions, refer to Getting Started with functions in Python.

AVOID TROUBLE

A function must be defined before the function is called in the script. For example, let's say you call the "foo" function before the foo function is defined.

#!/usr/bin/python
return_greeting("Hello World")

def return_greeting(greeting):
  print(greeting)

 

This will return an error that looks something like this because a function must be defined before the function is called.

Traceback (most recent call last):
  File "/home/john.doe/testing.py", line 3, in <module>
    Hello("World")
NameError: name 'Hello' is not defined

 

In this example, values "Hello" and then "World" are passed into the return_greeting function. The values passed into the return_greeting function are placed in the "greeting" variable.

#!/usr/bin/python

def return_greeting(greeting):
  print(greeting)

return_greeting("Hello")
return_greeting("World")

 

Here is how you would pass two (or more) values into a function.

#!/usr/bin/python

def return_greeting(one,two):
  print(one)
  print(two)

return_greeting("Hello", "World")

 

Let's say you do something like this, where the return_greeting function expects one argument but no arguments were passed in.

#!/usr/bin/python3
def return_greeting(greeting):
  print(greeting)

return_greeting(

 

This will cause the following Traceback.

Traceback (most recent call last):
  File "testing.py", line 5, in <module>
    return_greeting()
TypeError: return_greeting() missing 1 required positional argument: 'greeting'

 

Or like this, passing in two or more arguments.

def return_greeting(greeting):
  print(greeting)

return_greeting("Hello", "World")

 

This will cause the following Traceback.

Traceback (most recent call last):
  File "testing.py", line 5, in <module>
    return_greeting("Hello", "World")
TypeError: return_greeting() takes 1 positional argument but 2 were given

 

*args can be used to pass non-keyword arguments, which can be very useful in preventing exception NameError: global name 'my_variable' is not defined from being raised. This is kind of like passing in a list of values. For example, in this example values Hello and World are passed into the return_greeting function.

#!/usr/bin/python

def return_greeting(*args):
  if args:
    for argument in args:
      print(argument)

return_greeting("Hello", "World")

 

However, you'll probably instead almost always go with **kwargs to pass in keyword arguments, which is like passing in a dictionary of key/value pairs.

#!/usr/bin/python
     
def return_greeting(**kwargs):
  if kwargs:
    for key, value in kwargs.items():
      print(f"{key} = {value}")      

return_greeting(foo="Hello", bar="World")

 

Here is a bit of a more mature approach, especially if you are going to be passing in quite a few key/value pairs into the function.

def return_greeting(**kwargs):

  required_items = {
    'foo': '',
    'bar': ''
  }

  optional_items = {
    'foo': '',
    'bar': ''
  }

  # update the keys in required_items and optional_items to have values passed in from the caller script
  if kwargs:
    for key, value in kwargs.items():
      for xkey, xvalue in required_items.items():
        if key == xkey:
          required_items[xkey] = value
      for xkey, xvalue in optional_items.items():
        if key == xkey:
          optional_items[xkey] = value

  # return failed if any of the required_items contain no value (e.g. the key/value pair was not passed into this function)
  for key, value in required_items.items():
    if value == '':
      message = f"the {key} variable contains no value"
      return {"result": "failed", "message": message}

  # return failed if an invalid env was passed in
  if not re.match('dev|stage|prod', required_items['env']):
    message = f"{required_items['env']} is NOT a valid env. The valid env's are dev or stage or prod"
    return {"result": "failed", "message": message}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 797440 in the box below so that we can be sure you are a human.