I have a Python project where most business logic is in class methods. Now I would like to reuse some of the class methods in an independent project.
Is it possible to write a class method that 'exports' other class methods to a new Python file to create a script with a bunch of exported functions?
class MyObject:
def __init__(self, value):
self.value = value
def method1(self):
# the method I want to use in another project
def method2(self):
...
def method3(self):
...
def export_method(self, target_file):
# export the code of method1 to a new python file
When I run export_method('myfile.py')
I would like to create a Python file that contains method1
as a function:
def method1():
...
Note: I understand that the software should be restructured and method1
should be in another module where it can be imported from other projects. I'm just curious if there is a simple way to access the code of a Python program from the code itself.