0

I am very new to Python and OOP in general. I have a very easy question thou that just won't be working for me. I have a class with several functions.

class Test:

    def a(self):
        var1 = 1
        return var1

    def b(self):
        var2 = 2
        return var2

    def c(self):
        var3 = a()
        var4 = b()

Why will this just tell me that it does not know a() or b() I guess I am missing some important basic knowledge. Thanks

The Actual I am refering to:

class Token:
    # get new token
    def post_new_token(self):
        payload = "***"
        headers = {
            'authorization': "***",
            'content-type': "application/x-www-form-urlencoded"
        }
        r = requests.post(settings.GET_NEW_TOKEN, data=payload, headers=headers)
        r_json_obj = r.json()
        # print ("I am from post_new_token:") 
        # print r_json_obj
        return r_json_obj

    # use the new token
    def get_config(self):
        # here I want to use the returned value:
        access_token = self.post_new_token()
        headers = {
            'authorization': "Bearer " + str(access_token),
            'cache-control': "max_age 3600"
        }
        r = requests.get(settings.GET_CONFIG, headers=headers)
        # print ("I am from get_config:")
        # print access_token
        return r

So if use the prints it actually goes fine but when it gets to self.post_new_token() it runs all this function again.

There is no error coming but if I use the prints to see whats happening I get it like this:

I am from post_new_token:
{***}
I am from get_config:
{***}
I am from post_new_token:
{***}

Why is it printing

I am from post_new_token:
{***}

again?

10
  • You need to use self.a() and similarly for b. To know why, I suggest reading the tutorial section on classes. It would be well worth your time to read it over carefully and see where your misunderstand lies. Commented Mar 31, 2016 at 14:44
  • Ok I did that in my actual code. But than it goes throu the whole function and prints out what is in this function. But I just want to use the returned content and not everything from the function Commented Mar 31, 2016 at 14:46
  • And please don't refer to a "def". They are called functions, or - when they're within a class, as here - methods. Commented Mar 31, 2016 at 14:46
  • @buchstabe You need to provide the actual code that is giving you problems so we can actually know how to provide the proper solution. As is, the solution is simply applying the self when calling your instance methods. Update your question with what is actually going on and add the full stack trace you are receiving, if any. Commented Mar 31, 2016 at 14:47
  • 1
    Your actual code seems OK. Can you also post the actual error message (copy-paste, please, no retyping or summarizing)? Include any backtrace that might be in the error message. Commented Mar 31, 2016 at 14:56

3 Answers 3

2

You need to use self if you want to call an instance method or access a name (instance variable) from other instance methods.

def c(self):
    var3 = self.a()
    var4 = self.b()
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of:

def c(self):
    var3 = a()
    var4 = b()

I would try:

def c(self):
    var3 = self.a()
    var4 = self.b()

1 Comment

Good answer! Maybe you could add more prose describing the conventional use of self, the difference between local and global namespaces, or why a isn't in scope. Also, remember to indent correctly. If the code in an answer doesn't run, OP might not believe the rest of the answer.
1

Let's look at this case first:

class Test:
    def a(self):
        var1 = 1
        return var1

    def b(self):
        var2 = 2
        return var2

When you instantiate a class:

test_obj = Test()

And then try to see what a or b are:

print test_obj.a
>>> <bound method Test.a of <so.Test instance at 0x18B9B648>>
print test_obj.b
>>> <bound method Test.b of <so.Test instance at 0x18B9B648>>

Notice - it says bound method of an instance of Test. These methods are bound to an instance - you can only use them with that instance. Incidentally, have you ever wondered what self was and why you always have to pass it in to instance methods of a class? Did you know you could rewrite your class like this (not recommended, always use self for this, just doing this to illustrate the point).

class Test:
    def a(instance): #a belongs to that instance
        var1 = 1
        return var1

    def b(instance):
        var2 = 2
        return var2

And it would work the same way? If you also added print instance to a, like so:

def a(instance):
    print instance
    var1 = 1
    return var1

And tried printing out the method a and calling the method:

print test_obj.a
>>> <bound method Test.a of <so.Test instance at 0x18B9B3C8>> # Notice memory address
test_obj.a() # Remember we added print instance here
>>> <so.Test instance at 0x18B9B3C8> # Address is the same

This was just to illustrate the point that these methods are bound to an instance, and to use them you need to use that specific instance, thus:

class Test:
    # This function is in the scope of the class
    def a(self):
        var1 = 1
        return var1

    # This function is in the scope of the class
    def b(self):
        var2 = 2
        return var2

    def c(self):
        # a() and b() are not defined in this scope,
        # to use them you would have to define them again here (within the scope of c).
        # def a():
        #     return 1
        # def b():
        #     return 2
        # Your methods are bound to self (current instance)
        var3 = self.a()
        var4 = self.b()

And when calling c you would again, use the instance - test_obj.c()

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.