Importing methods for a Python class
import, methods, python
Solution
I don't think what you want is directly possible in Python.
You could, however, try one of the following.
- When generating `to_import_from.py`, add the non-generated stuff there too. This way, all methods are in the same class definition.
- Have `to_import_from.py` contain a base class definition which the the Instrument class inherits.
In other words, in `to_import_from.py`:
class InstrumentBase(object):
def external_method(self, arg1, arg2):
if self.flag:
...
and then in `main_module.py`:
import to_import_from
class Instrument(to_import_from.InstrumentBase):
def __init__(self):
...
Problem
I wonder if it's possible to keep methods for a Python class in a different file from the class definition, something like this: `main_module.py:` ``` class Instrument(Object): # Some import statement? def __init__(self): self.flag = True def direct_method(self,arg1): self.external_method(arg1, arg2) ``` `to_import_from.py:` ``` def external_method(self, arg1, arg2): if self.flag: #doing something #...many more methods ``` In my case, `to_import_from.py` is machine-generated, and contains many methods. I would rather not copy-paste these into main_module.py or import them one by one, but have them all recognized as methods of the Instrument class, just as if they had been defined there: ``` >>> instr = Instrument() >>> instr.direct_method(arg1) >>> instr.external_method(arg1, arg2) ``` Thanks!