1

I'm working on a project and I need to import a variable that I got in a function and use it in another one. Here's an example of what I'm trying to do.(this is not my code, just an example)

     def interest(day,month,year):
        interest = x          #I got X by requesting a web data base.

     def calc(debt,time):
        result = (debt*x)/time

Is there a way to import X from one function to another?

Thanks!

4
  • 2
    Where is x declared? Commented Aug 27, 2015 at 5:06
  • By import are you trying to transfer from one function to other function on the same script Commented Aug 27, 2015 at 5:08
  • x is a result I got from the first function, it is just a literal example, I need to import a result I got in the first function and use in the second function. Commented Aug 27, 2015 at 5:10
  • @VigneshKalai yeah, I got a result in the first function (its a variable interest rate ) and I need to use it in the second function (that calculate how much interest the client will pay) Commented Aug 27, 2015 at 5:14

4 Answers 4

2

Have you considered using a class? Classes are extremely useful if you have variables that must be passed around to many functions.

class Foo:
    def __init__(self):
        pass

    def set_x(self):
        self.x = 5 # Whatever your source is!

    def calculate_something(self, foo, bar):
        return foo * bar * self.x

q = Foo()
q.set_x()
print(q.calculate_something(2, 3))

Output: 30

In case you aren't interested in using classes you could do the following:

  1. Fetch x.
  2. Pass it to both interest and calc rather than fetching it within interest
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, but saying "Classes are extremely useful if you have variables that must be passed around to many functions" is like saying that "goto is extremely useful if you have code that must be run in many different places"
0

You don't import variables from a function to another. Functions are for returning values and variables, and you can always call a function to get its return value. For example:

def interest(amt):
    return amt * FIXED_INT

def calc(debt, time):
    return debt * interest(debt) ** time

Comments

0

You can pass your variable into arguments

 def interest(day,month,year):
    interest = x
    return interest


 def calc(debt,time, interest):
    result = (debt*interest)/time

 ...     

 x = interest(day,month,year)
 calc(debt,time, x)

Comments

0

Here's how I did it:

def grades_sum(scores):
    sum1 = 0
    for i in scores:
        sum1 = sum1 + i
    print sum1
    return sum1

grades_sum([100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5])

def grades_average(grades):

    average = grades_sum(grades)/float(len(grades))

    print average

    return average

grades_average([100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5])

Basically, I returned sum1 in the first function, so that when I call it in the second function, sum1 will be divided by float(len(grades)). Hope I helped! P.S. Apologies for the ugly formatted text, this is my first post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.