Reputation: 445
Say 'Mathematics' is a Module downloadable from Pypi. It contains the following function:
def addition(a, b):
global mul
mul = a * b
sum = a + b
return sum # only returns sum, mul is not returned
I call this function using import like so -
from mathematics import addition
ans1 = addition(3, 5)
print(ans)
But when I say -
ans2 = mul
I get the following error:
AttributeError: 'function' object has no attribute 'mul'
I was hoping to retrieve the value of 'mul' as it has been assigned to be global. How can I retrieve mul from an imported function. Had it not been imported, 'mul' would work. Since the function is imported, obviously the source code of the function 'addition' is not available.
What should be done?
Upvotes: 0
Views: 65
Reputation: 356
if you want you can return the value of mul along with add but actually that doesn't make sense....
but if you want to access the value of mul, do
from mathematics import addition,mul
mul must be a global var though.
Upvotes: 0
Reputation: 42778
Instead of using global, return both values:
def addition(a, b):
mul = a * b
sum = a + b
return sum, mul
and assign both to two variables:
ans1, ans2 = addition(3, 5)
Upvotes: 1