3

this is a simplified example of the problem I have. I would like to call var1 in func2, is this possible?

class Plot():
    def __init__(self,variable):
        self._variable = float(variable)

    def func1(self):
        var1 = self._variable + 1
        print(var1)

    def func2(self):

        var2 = var1 + 1
        print(var2)

the example commands I have tested

h = Plot(3)

h.func1()
h.func2()

and understandably the last command raises issues.

dflyn
  • 31
  • 2

1 Answers1

6

You need to assign a 'self' in front of var1 inside your func1.

So, do this:

def func1(self):
    self.var1 = self._variable + 1
    print(var1)

That way in func2, you just do this:

def func2(self):

    var2 = self.var1 + 1
    print(var2)

An explanation of why exactly this is required.

**Edit: Thanks to @Pynchia for the comments

Let us look at your code to further explain why this is needed:

class Plot():
    def __init__(self,variable):
        self._variable = float(variable)

    def func1(self):
        self.var1 = self._variable + 1
        print(var1)

    def func2(self):
        var2 = self.var1 + 1
        print(var2)

In short, because it is a very detailed topic, the usage of self allows you to use your variables in the instance namespace, in short, they can be used across your methods, like in the solution I proposed.

So, when you end up creating an instance of your class:

obj = Plot()

Your obj is now in the instance (object) space/scope, and the way to make "stuff" available to that obj is by the usage of self in how everything was defined in the above example.

To really understand what is going on, a much longer explanation would be required, and I think the following link really does the description justice, and I strongly recommend reading it:

http://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide

Here is another SO post that will explain this as well:

What is the purpose of self?

Community
  • 1
  • 1
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • 1
    It would be nice to explain why; although I do understand it may require a lengthy description – Pynchia Oct 12 '15 at 03:09
  • 1
    I was actually editing my post explaining. Few minutes. :) – idjaw Oct 12 '15 at 03:10
  • @Pynchia please let me know what you think. Constructive criticism is welcome. :) – idjaw Oct 12 '15 at 03:40
  • 1
    I think it's not very clear. Maybe the point to highlight is the fact that python has no variable declaration so where the assignment takes place is important and defines the scope of the name as well. Furthermore, when referenced in an object instance (i.e. through `self.`) a name (read variable) is looked up in the instance namespace, and if not found, in the class' namespace, then in globals... I told you it'd be tedious :) – Pynchia Oct 12 '15 at 12:00
  • 1
    @Pynchia thank you for the review, really appreciate it. It is a very hard topic to summarize in a short post, you are absolutely right. I re-wrote everything, hopefully it is simpler. I also opted to reference a pretty well written article that helps articulate this in a good amount of detail to help really illustrate this. Hopefully it is more clear now. Thanks again. – idjaw Oct 12 '15 at 13:03