Importing identically named methods
<p>So I have two separate Python packages that I'm importing into my Python script for Raspberry Pi. In this case as;</p>

<pre><code>from rflib import*
from Rpi.GPIO import*
</code></pre>

<p>However, both packages have their own seperate method <code>cleanup(self)</code></p>

<p>So, at the end of the script when I use the command
<code>cleanup()</code>, how do I a) know which package the method is coming from (they both do utterly different things b) control which one is run?</p>

<p>I've looked into similarly named <a href="http://stackoverflow.com/questions/21482427/two-classes-with-a-structurally-identical-method-but-one-differently-named-varia">questions</a>, but which seem to deal with inheritance overloading rather than package imports</p>

<p><code>from &lt;module&gt; import *</code> takes all names in a module (those that don't start with a <code>_</code> or everything listed in <code>&lt;module&gt;.__all__</code> and assigns those names as globals in your current module.</p>

<p>If two modules define the same name, that means that the last one imported this way wins; <code>cleanup = rflib.cleanup</code> is replaced by <code>cleanup = Rpi.GPIO.cleanup</code> with the second <code>from Rpi.GPIO import *</code> statement.</p>

<p>You generally want to <em>avoid</em> using <code>from &lt;module&gt; import *</code>. Import specific names, or just the module itself.</p>

<p>In this case, you can do:</p>

<pre><code>from rflib import cleanup as rflib_cleanup
from Rpi.GPIO import cleanup as rpigpio_cleanup
</code></pre>

<p>which would bind those two functions as <em>different</em> names.</p>

<p>Or you could just import the modules:</p>

<pre><code>import rflib
from Rpi import GPIO
</code></pre>

<p>which gives you only the <code>rflib</code> and <code>GPIO</code> names, each a reference to the module object, so now you can reference each of the <code>cleanup</code> functions as attributes on those modules:</p>

<pre><code>rflib.cleanup()
GPIO.cleanup()
</code></pre>

<p>If you need to use a lot of names from either module, the latter style is preferred because that limits the number of imports you need to do, keeps your namespace clean and un-cluttered, and gives you more context whereever those names are used in your code.</p>

python python-import