0

Let's say I have a function:

func1():
    do something
    a,b = somevalues,somevalues

Now let's say I have another function:

func2():
    do something

I would now like to now use a,b in func2() which were calculated inside func1() .

I tried using

func1():
    do something
    a,b = somevalues, somevalues
    func1.a = somevalues
    func1.b = somevalues

func1()
func1.a

But using this, I have to each time run func1() before using a,b.

func1() also creates some plots along with calculating a,b.

So is it possible to utilise a,b in func2() without calling func1() ?

10
  • Can you return a,b from func1() and then pass them as arguments to func2()? Commented Sep 21, 2015 at 12:33
  • @robert: I don't know!! Can you please elaborate? Commented Sep 21, 2015 at 12:34
  • 1
    You need to rethink your code structure. If you need a and b independently of func1() then they should not be declared within it. Commented Sep 21, 2015 at 12:34
  • @michaelrccurtis: But I calculate them inside the func1() as I needed them for several plots inside func1(). So you are suggesting me to calculate them outside the function? Commented Sep 21, 2015 at 12:35
  • 1
    @ThePredator Yes, exactly. You could store them as member variables of a class, for example, if that makes sense given their meaning. Commented Sep 21, 2015 at 12:36

3 Answers 3

5

This may be what you want. Also, I suggest you work through an introductory Python tutorial.

func1():
    #do something
    a,b = somevalues, somevalues
    return a, b

func2(a, b):
    #do something on a and b

a, b = func1()
func2(a, b)
Sign up to request clarification or add additional context in comments.

1 Comment

I accept this as an answer for now! But as pointed out by @michaelrccurtis, I shall try to restructure my code!
2

The answer by robert is probably the easiest way to do what your want. Here is another:

class class1():
    a,b = somevalues, somevalues
    @staticmethod
    def func1():
        do something

func2(class1.a, class1.b)
class1.func1()

The reason this works and your way doesn't work has to do with the way Python treats functions and classes.

The body of a function is not executed until it is called the first time. So when you have:

def func1():
    func1.a = somevalue

...you cannot access func1.a until func1 has been called at least once, as you have already discovered.

However, for classes, the body of the class runs when the code is compiled. So that when you do:

class example:
    a = somevalue

...you are able to access example.a immediately.

EDIT:

Answering the question in the comments: access func1, a, or b as shown above using the class itself (in Python, classes ARE objects, just like any other object):

class1.a
class1.func1()

You could also make a shortcut for yourself:

func1 = class1.func1
func1()

Another way to do things- so that you could have different versions of a and b- be to make a and b instance attributes instead of class attributes.

class class1:
    def __init__(self, a, b):
        self.a, self.b = a, b
    @staticmethod
    def func1():
        dosomething

obj1 = class1(somevalue1A, somevalue1B)
obj2 = class1(somevalue2A, somevalue2B)
func2(obj1.a, obj1.b)
func2(obj2.a, obj2.b)
obj1.func1()
obj2.func1()
class1.func1()

The last three lines all call the same function, because obj1.func1, obj2.func1, and class1.func1 all point to the same method of class1. Note that the reason you can call func1 from both the class (class1) and the instances (obj1, obj2) of the class is that func1 is a static method.

4 Comments

I understand now! But in this case, how do I call func1() ? Do I need to use the class and call it or can I call it directly like func1() ?
You can use the class to call it since it is a static method of the class, or you can make an instance of the class. I'll add some more explanation.
@ThePredator I have answered your question in the answer.
Thank you for your detailed explanation! I shall try to understand it slowly
0
func1():
    do something
    a,b = somevalues,somevalues
    return a, b

x, y = func1()

Now you can use x and y everywhere without callin func1() every time. For example in func2() this way:

def func2(a, b):
    do something

Then you call func2() with x and y respectivly:

func2(x, y)

2 Comments

but since func1() create many plots, I don't want to call func1() to access the variables as I need to then close all the plots!
Then why do you assign values to a and b in a function that draws a plot?

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.