Python objects in other classes or separate?
<p>I have an application I'm working on in <strong>Python 2.7</strong> which has several classes that need to interact with each other before returning everything back to the main program for output.  </p>

<p>So a brief example of the code would be:</p>

<pre><code>class foo_network():
    """Handles all network functionality"""
    def net_connect(self, network):
       """Connects to the network destination"""
       pass

class foo_fileparsing():
    """Handles all sanitation, formatting, and parsing on received file"""
    def file_check(self, file):
        """checks file for potential problems"""
        pass
</code></pre>

<p>currently I have a main file/function which instantiates all the classes and then handles passing data back and forth, as necessary, between them and their methods.  However this seems a bit clumsy.</p>

<p>As such I'm wondering two things:</p>

<ol>
<li>What would be the most 'Pythonic' way to handle this?</li>
<li>What is the best way to handle this for performance and memory usage?</li>
</ol>

<p>I'm wondering if I should just instantiate objects of one class inside another (from the example, say, creating a <strong>foo_fileparsing</strong> object within the <strong>foo_network</strong> class if that is the only class which will be calling it, rather than my current approach of returning everything to the main function and passing it between objects that way.</p>

<p>Unfortunately I can't find a PEP or other resource that seems to address this type of situation.</p>

<p>You can use modules. And have every class in one module.
and then you can use import to import only a particular method from that class.
And all you need to do for that is create a directory with the name same as you class name and put a <strong>init</strong>.py file in that directory which tells python to consider that directory as a module. 
then for ex. foo_network folder contains a file named foo_network.py and a file <strong>init</strong>.py. and in foo_network.py the code is- </p>

<pre><code>class foo_network():
    """Handles all network functionality"""
    def net_connect(self, network):
       """Connects to the network destination"""
       pass
</code></pre>

<p>and in any other file you can simply use </p>

<pre><code>import net_connect from foo_network
</code></pre>

<p>it will only import that particular method. This way your code will not look messy and you can will be importing only what is required. 
you can also do </p>

<pre><code>import * from foo_network 
</code></pre>

<p>to import all methods at once. </p>

<p>Hope that helps.</p>

python oop memory-management python-performance