Andrew
Andrew

Reputation: 1126

How to print the values of parameters passed into a function

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?

Upvotes: 0

Views: 2646

Answers (4)

Nathan
Nathan

Reputation: 3190

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.

Upvotes: 3

vivek kumar
vivek kumar

Reputation: 61

Please try the following code. It uses the concept of closure (google it). Hope it is helpful.

def times():
    value = float(input('Enter a value:'))
    power = float(input('Enter a power: '))

    def raise_to_power():
        return value ** power
    print(
        f'Result of calculation is: {raise_to_power(): .6f}\nValue passed to function was: {value}\nPower passed to function was: {power}')


times()

Upvotes: 0

António Rebelo
António Rebelo

Reputation: 304

You can always just add more "print" lines.

So the code would look something like this:

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

and then you can just pass the values into the function like so:

times(2.72, 3.1)

Upvotes: 0

Amine Slama
Amine Slama

Reputation: 56

You will need to first store the parameters in variables in the code that calls the function.

Assuming the function 'times' is defined.

a = 2.72
b = 3.1
x = times(a, b)
print(f'Result of calculation is: {x: .6f}')
print(f'Value passed to function was: {a}')
print(f'Power passed to function was: {b}')

Upvotes: 0

Related Questions