How to deal with a lot variables access in different function/class in python
Question: The code which I wanna improved now looks something like below,  
which f0 and f1(or more than 2 function) need the same variables.  
I have to code about 50 lines to describe the variable setting at each
function. how can I do this more pythontic?

\--f0.py

    
    
    import csv
    def gen(csv_f): 
        # var define
        for row in csv.DictReader(open(csv_f)):
            c += row['x']
            ...
        a  = 1
        b  = 2
        ...
        # do sth in f0
        xx = a + b
        ...
        str = ...
        return str
    

\--f1.py

    
    
    import csv  
    def gen(csv_f): 
        # var define
        for row in csv.DictReader(open(csv_f)):
            c += row['x']
            ...
        a  = 1
        b  = 2
        ...
        # do sth in f1
        xx = a*b + b
        ...
        str = ...
        return str
    

\--genstr.py

    
    
    from f0 import *
    from f1 import *
    
    f = open('xxx.x','w')
    f.write(f0.gen(1)+f1.gen(1))
    f.close()
    

* * *

(I don't really know how to use class, but I found this could help my problem  
just describe maybe it will help understanding my question )  
I try to do it with class, so i can access by inherit conf.  
I know I can access by 'self.a', but is there any way I can direct use 'a' in
the function?

\--conf.py

    
    
    class conf:
        def __init__(self, csv_f):
            # var define
            for row in csv.DictReader(open(csv_f)):
                c += row['x']
                ...
            self.a  = 1
            self.b  = 2
            ...
    

\--f0.py

    
    
    import conf
    class f0(conf):
        def __init__(self):
            config.__init__(self, csv_f) #this line is not correct
        def gen():
            # var set
            c = self.c
            a = self.a
            b = self.b
            # do sth in f0
            xx = a + b
            ...
            str = ...
            return str
    

\--f1.py

    
    
    import conf
    class f1(conf):
        def __init__(self):
            config.__init__(self, csv_f) #this line is not correct
        def gen():
            # var set
            c = self.c
            a = self.a
            b = self.b
            # do sth in f1
            xx = a + b
            ...
            str = ...
            return str
    

\--genstr.py

    
    
    from f0 import *
    from f1 import *
    
    f = open('xxx.x','w')
    f.write(f0.gen(1)+f1.gen(1))
    f.close()
    


Answer: The code is slightly confusing and i'm not sure what exactly you are trying,
but here are some general help.

This code:

    
    
    for row in csv.DictReader(open(csv_f)):
        c += row['x']
    

Will append the content of coloumn `x` to `c`.

    
    
    import conf
    class f0(conf):
        def __init__(self, csv_f):
             super(f0,self).__init__(self)  # Fixed
             self.csv_f = csv_f
    
        def gen(self):
             # var set
             c = self.c # 
             a = self.a
             b = self.b
             # do sth in f0
             xx = a + b
    

Instead of `c = self.c` you can use `self.c` where ever you need `c`.

    
    
    from f0 import *
    from f1 import *
    
    f = open('xxx.x','w') 
    f.write(f0(filename).gen()+f1(filename).gen())
    f.close()
    

