繁体   English   中英

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

[英]Python: Call a variable from another function within a class

一般来说,我对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:
{***}

再次?

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

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

代替:

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

我会尝试:

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

让我们先来看这种情况:

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 instancea ,就像这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM