-1

I am trying to pass two lists to other functions. in one function it is executing as I am calling the total function using self. But I shouldn't call the same function in another method too, as the git clone will happen twice. Hence I need to pass the function return values to other function. Is there any way to do this? This total code I am writing in single class.

def open_close_lists(self):
    cmd = self.git_clone
    subprocess.check_output(cmd, shell=True)
    open_list = os.listdir('open')
    close_list = os.listdir('close')
    return open_list, close_list

I have another two functions

def fun1(self):
    here I need to use those two lists
def fun2(self):
    here I need to use those two lists
martineau
  • 119,623
  • 25
  • 170
  • 301
SinghB
  • 29
  • 1
  • 5
  • 1
    What does your class represent? Does it not make sense to add the two lists as instance attributes (`self.open_list` and `self.close_list`)? – Carcigenicate Aug 25 '20 at 14:04
  • Does this answer your question? [Passing variables between methods in Python?](https://stackoverflow.com/questions/9520075/passing-variables-between-methods-in-python) – mkrieger1 Aug 25 '20 at 14:09
  • Hi, I tried this its giving attribute error as follows: rel_two = self.matchRel2(self.open_list, self.close_list) AttributeError: 'TestMatch' object has no attribute 'open_list' – SinghB Aug 25 '20 at 14:11

2 Answers2

3

Here we create a class OpenCloseLists, and bind those lists to current instance.

Lists are now available using the self keyword.

NOTE: It's common to name classes in CamelCase and variable names with underscores.

import os

class OpenCloseLists:
    def __init__(self):
        cmd = self.git_clone
        subprocess.check_output(cmd, shell=True)
        self.open_list = os.listdir('open')
        self.close_list = os.listdir('close')
      
    def fun1(self):
        # self.open_list
        # self.close_list
    
    
    def fun2(self):
        # self.open_list
        # self.close_list
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
0

Use global variables. At the start of your function put

global self.mylist
Dharman
  • 30,962
  • 25
  • 85
  • 135
MusicMan24
  • 51
  • 1
  • 10