-7

I have 2 python programs namely a1.py, a2.py

I want to import a1.py in a2.py.

Here are the sample codes: a1.py:

class a:
    def __init__(self):
        #code
    def __str__(self):
        #code
    def f1(self):
        #code
    def f2(self,file):
        #code to read a file
          and call f1 to do operations
        #returns final text as str 
m = a()
m.f2("file")

a2.py

class b:
    def __init__(self):
        #code
    def __str__(self)
        #code
    def f3(self,text) #this text is the output of a1.py
        #code
    def f4(self)
        #code gives the final output as list and calls f3

n = b()
n.f4()

How can I use the output of a1.py in a2.py?

martineau
  • 119,623
  • 25
  • 170
  • 301
ramu suri
  • 9
  • 5
  • Possible duplicate of [Call a function from another file in Python](https://stackoverflow.com/questions/20309456/call-a-function-from-another-file-in-python) – Ken Y-N Oct 08 '19 at 08:33
  • Please, if you are including Python code, ensure it is indented correctly; the code as is will not run. – Ken Y-N Oct 08 '19 at 08:34
  • Your `a1.py` and `a2.py` are **not valid** [`modules`](https://docs.python.org/3/tutorial/modules.html#executing-modules-as-scripts), read about: `if __name__ == "__main__":` and [Python - Object Oriented](https://www.tutorialspoint.com/python/python_classes_objects.htm) – stovfl Oct 08 '19 at 09:53

1 Answers1

2

Do you have to have

m = a()
m.f2("file")

In a1.py?

It might be easier to have a2.py do:

from a1 import a

class b:
    def __init__(self):
        #code

    def __str__(self)
        #code

    def f3(self,text) #this text is the output of a1.py
        #code

    def f4(self)
        #code gives the final output as list and calls f3

n = b()
n.f4()
m = a()
m.f2("file")
rdas
  • 20,604
  • 6
  • 33
  • 46
Zhi Yong Lee
  • 149
  • 4