Python:从类中的另一个函数调用变量

时间:2016-03-31 14:42:35

标签: python

我对Python和OOP很新。我有一个非常简单的问题,你不会为我工作。 我有一个有几个功能的课程。

class Test:

    def a(self):
        var1 = 1
        return var1

    def b(self):
        var2 = 2
        return var2

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

为什么这只是告诉我它不知道a()或b() 我想我错过了一些重要的基础知识。 感谢

我所指的实际:

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

所以如果使用打印它实际​​上很好但是当它到达self.post_new_token()时它会再次运行所有这个函数。

没有错误,但如果我使用打印件来查看发生了什么,我会这样:

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

为什么打印

I am from post_new_token:
{***}

再次?

3 个答案:

答案 0 :(得分:2)

如果要调用实例方法或从其他实例方法访问名称(实例变量),则需要使用self

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

答案 1 :(得分:1)

而不是:

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

我会尝试:

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

答案 2 :(得分:0)

让我们先看看这个案例:

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

    def b(self):
        var2 = 2
        return var2

实例化一个类时:

test_obj = Test()

然后尝试查看ab是什么:

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>>

注意 - 它表示Test实例的绑定方法。这些方法绑定到一个实例 - 您只能将它们与该实例一起使用。顺便说一句,您有没有想过self是什么以及为什么总是要将它传递给类的实例方法?你是否知道你可以像这样重写你的课程(不推荐,总是使用 self 为此,只是为了说明这一点)。

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

    def b(instance):
        var2 = 2
        return var2

它会以同样的方式工作吗?如果您还将print instance添加到a,请执行以下操作:

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

尝试打印方法a并调用方法:

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

这只是为了说明这些方法绑定到一个实例,并使用它们需要使用该特定实例,因此:

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

当您再次呼叫c时,请使用实例 - test_obj.c()

相关问题