我在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!'
我想避免将这两种方法合并为一种,因为这会破坏我的文件,但我会考虑在必要时更改静态方法的方法类型。
答案 0 :(得分:1)
您无法从被调用函数访问本地命名空间 - 但您可以轻松地从任何被调用函数访问返回值。
class Analytics:
@staticmethod
def UsersPerCountryOrPlatform():
...
return UsersPerCountry, UsersPerPlatform
class UsersPlatformPie:
def compute_figure(self):
myUsersPerCountry, myUsersPerPlatform = Analytics.UsersPerCountryOrPlatform()
print(myUsersPerCountry)
print(myUsersPerPlatform)