在类中,从一个函数中访问另一个函数中的Dictionary值

时间:2023-01-01 16:00:09

I have a module which has a class and two functions in it. func2 is invoked first and it creates dictionaries from SQL output. I want to access values in these dictionaries to write it to file inside func1.

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

I tried to search for answers but couldn't locate my scenario so posting this question.

我试图搜索答案,但找不到我的方案,所以发布这个问题。

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 个解决方案

#1


0  

Like juanpa.arrivillaga states, you can either pass the dictionaries as arguments, or make them class/object attributes. Since you are not returning them I'll assume that they should be class attributes. This can be done as shown below. I'll be honest though, I don't believe that you looked hard enough before asking this question as this would be found in any class tutorial.

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

import sys
import os 
import abc as var

class test():
    tgt_dict1 = {}
    tgt_dict2 = {}        
    tgt_dict3 = {}

    def func1(self,A,B,C):
            csv=sample.csv
            print>>csv,"Value in dict from other function,",test.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 receive
            #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)  

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

#1


0  

Like juanpa.arrivillaga states, you can either pass the dictionaries as arguments, or make them class/object attributes. Since you are not returning them I'll assume that they should be class attributes. This can be done as shown below. I'll be honest though, I don't believe that you looked hard enough before asking this question as this would be found in any class tutorial.

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

import sys
import os 
import abc as var

class test():
    tgt_dict1 = {}
    tgt_dict2 = {}        
    tgt_dict3 = {}

    def func1(self,A,B,C):
            csv=sample.csv
            print>>csv,"Value in dict from other function,",test.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 receive
            #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)  

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