在一个类中,从另一个

时间:2018-06-17 22:48:43

标签: python

我有一个模块,里面有一个类和两个函数。 首先调用func2,它从SQL输出创建字典。 我想访问这些词典中的值,将其写入func1中的文件。

我试图搜索答案,但无法找到我的方案,因此发布了这个问题。

import sys
import os 
import abc as var

class test():

        def func1(self,A,B,C):
                csv=sample.csv
                print>>csv,"Value in dict from other function,",<not sure how to call>.tgt_dict3['table_name']
                <do something else>

        def func2(self,x,y,z):
                <do something not related to issue>
                #Call function collect_var and pass same arguments which this function received
                #This function takes input and runs query on SQL and returns dictionaries with column name and values
                (dict1,dict2,dict3)=var.collect_var(x,y,z)  

                 tgt_dict1=dict1[0]
                 tgt_dict2=dict2[0]
                 tgt_dict3=dict3[0]

1 个答案:

答案 0 :(得分:0)

与juanpa.arrivillaga状态一样,您既可以将字典作为参数传递,也可以将它们作为类/对象属性。既然你没有返回它们,我会假设它们应该是类属性。这可以如下所示完成。我会说实话,我不相信你在问这个问题之前看起来很难,因为这可以在任何课程教程中找到。

{{1}}