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