Bootstrap FreeKB - Python (Scripting) - Return value in a function
Python (Scripting) - Return value in a function

Updated:   |  Python (Scripting) articles

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

Let's say you have a function named "Hello". Here is how you would return "Hello World" when the Hello function is called.

#!/usr/bin/python

def Hello():
  return "Hello World"

print Hello()

 

Or like this.

#!/usr/bin/python

def Hello():
  return "Hello World"

result = Hello()
print(result)

 

In this example, running the Python script should print "Hello World".

~]$ python testing.py
Hello World

 

More commonly, either a dictionary or JSON is returned. For example, to return a dictionary.

#!/usr/bin/python3

def Hello():
    return {"foo":"bar"}

result = Hello()

print(type(result))
print(result['foo'])

 

Will print the following.

<class 'dict'>
bar

 




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 a86816 in the box below so that we can be sure you are a human.