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() ?

Srivatsan
  • 9,225
  • 13
  • 58
  • 83
  • Can you return `a,b` from `func1()` and then pass them as arguments to `func2()`? – robert Sep 21 '15 at 12:33
  • @robert: I don't know!! Can you please elaborate? – Srivatsan Sep 21 '15 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. – michaelrccurtis Sep 21 '15 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? – Srivatsan Sep 21 '15 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. – michaelrccurtis Sep 21 '15 at 12:36
  • Your question is nonsensical. Why would you be able to use them before calling `func1`? Functions are not classes. You very much need to rethink how you are doing things. – Rick Sep 21 '15 at 12:40
  • @michaelrccurtis: If possible could you suggest me any links where I can read more about storing them as member variables of a class? – Srivatsan Sep 21 '15 at 12:40
  • Ive never seen that contruct before, but maybe a generator would suit you? The variables within generator functions maintain their values in between calls. – hilcharge Sep 21 '15 at 12:41
  • @ThePredator It's not EXACTLY what you're looking for, but try reading my answer to a previous question. http://stackoverflow.com/questions/68645/static-class-variables-in-python/27568860#27568860 – Rick Sep 21 '15 at 12:41
  • But if `a,b` are calculated witin `func1`, how can you use them without calling `func1`? 'Utilize' them in what way? – hilcharge Sep 21 '15 at 12:45

3 Answers3

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)
robert
  • 33,242
  • 8
  • 53
  • 74
  • I accept this as an answer for now! But as pointed out by @michaelrccurtis, I shall try to restructure my code! – Srivatsan Sep 21 '15 at 12:43
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.

Community
  • 1
  • 1
Rick
  • 43,029
  • 15
  • 76
  • 119
  • 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()` ? – Srivatsan Sep 21 '15 at 12:56
  • 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. – Rick Sep 21 '15 at 12:58
  • @ThePredator I have answered your question in the answer. – Rick Sep 21 '15 at 13:07
  • Thank you for your detailed explanation! I shall try to understand it slowly – Srivatsan Sep 21 '15 at 13:08
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)
Psytho
  • 3,313
  • 2
  • 19
  • 27
  • 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! – Srivatsan Sep 21 '15 at 12:39
  • Then why do you assign values to `a` and `b` in a function that draws a plot? – Psytho Sep 21 '15 at 12:47