[SOLVED] How to print the values of parameters passed into a function

Issue

I don’t know how to retrieve, store and print the values of parameters passed into a function. I do know that many posts are related to this question, but I couldn’t find anything that matches the simple thing I would like to do.

Let’s take a very simple example:

def times(value, power):
    return value**power

If I run this function and then write:

x = times(2.72, 3.1)
print(f'Result of calculation is: {x: .6f}')

then the output will be:

Result of calculation is:  22.241476

OK, but this is not what I would like to have; I would like to be able to print the result, the value and the power, and have the following lines as output, preferably using a print as above; something like print(f’some text here: {something}’)

Desired output:

Result of calculation is:  22.241476
Value passed to function was:  2
Power passed to function was:  3

What is the most effective way to do that?

Solution

The question appears to be asking about accessing the function’s namespace, not just printing the value of the variables. If the namespace concept is new to you, I recommend reading the Python documentation and Real Python’s blog post on Namespace’s in Python. Let’s look at a few ways to do what you are asking.

Printing the values is straightforward:

def times(value, power):
    print(f"Value passed to function was:  {value}")
    print(f"Power passed to function was:  {power}")
    print(f'Result of calculation is: {x: .6f}')

If you need to print it out the way you describe in your question, the values should be returned. This can be accomplished by updating your function to:

def times(value, power):
    return value, power, value**power

v, p, result = times(2,3)

print(f'Result of calculation is: {result: .6f}')
print(f"Value passed to function was:  {v}")
print(f"Power passed to function was:  {p}")

However, returning parameters seems a little odd since one would assume you as the developer can capture those values elsewhere in your code. If you want to view the variables and their values for a given namespace, use the corresponding function. For viewing the value and power variables, which live in the function times() local namespace, use locals() which returns a dictionary object that is a copy of the current local namespace.

def times(value, power):
    print(locals())
    return value**power

>>> times(5, 4)
{'value': 5, 'power': 4}
625

If the variables are defined in the global namespace, (keep in mind global variables should be used with care) you can use globals() to look up the value in the global namespace:

VALUE = 2
POWER = 3
def times(value=VALUE, power=POWER):
    return value**power

>>> globals()['VALUE']
2
>>> globals()['POWER']
3

I hope this helps you figure out how to accomplish what you are working on. I recommend taking some time to read about how Python views and manages namespaces. If you want to watch a video, check out this PyCon talk by Raymond Hettinger on object oriented programming 4 different ways.

Answered By – Nathan

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.