簡體   English   中英

Python-從另一類的(靜態)方法調用變量

[英]Python - Calling Variable From (Static) Method of One Class in Method of Another Class

我正在PyQt中制作GUI,需要將腳本的圖形創建類和方法與其分析類方法相連接。 我嘗試通過簡單地從圖形方法中調用分析方法來執行此操作(如下所示),但這會導致“未定義全局名稱'UsersPerPlatform'”錯誤,因此,這顯然不會從其他方法中提取字典方法。

class Analytics():

    @staticmethod
    def UsersPerCountryOrPlatform():
        ...
        return UsersPerCountry
        return UsersPerPlatform #both are dictionaries

class UsersPlatformPie(MyMplCanvas): #irrelevant parent

    def compute_figure(self):
        Analytics.UsersPerCountryOrPlatform() #running function to return UsersPerPlatform
        for p, c in UsersPerPlatform:
            print 'If I could access the UsersPerPlatform dictionary I would plot something!'

我想避免將兩種方法合而為一,因為這會使我的文件混亂,但如有必要,我將考慮更改靜態方法的方法類型。

您不能從被調用函數訪問本地名稱空間-但是您可以輕松地從任何被調用函數訪問返回值。

class Analytics:
    @staticmethod
    def UsersPerCountryOrPlatform():
        ...
        return UsersPerCountry, UsersPerPlatform

class UsersPlatformPie:
    def compute_figure(self):
        myUsersPerCountry, myUsersPerPlatform = Analytics.UsersPerCountryOrPlatform()
        print(myUsersPerCountry)
        print(myUsersPerPlatform)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM