title: Dependencies. On loading modules for use in a function or a class, steer me?
body: <p>I am working my way through some samples, and came up with this working answer - the challenge being set in the doctests:</p>
  
  <pre><code>def remove(sub, s):
      """
        &gt;&gt;&gt; remove('an', 'banana')
        'bana'
        &gt;&gt;&gt; remove('cyc', 'bicycle')
        'bile'
        &gt;&gt;&gt; remove('iss', 'Mississippi')
        'Missippi'
        &gt;&gt;&gt; remove('egg', 'bicycle')
        'bicycle'
      """
      if sub not in s: return s # 1
      from string import replace # 2
      return replace( s, sub, '', 1) # 3
  </code></pre>
  
  <p>As a PyNoob I'd like to ask a few questions to get my bearings on all of this stuff vs the baggage I brought along with me from PHP.</p>
  
  <p>On my line:</p>
  
  <ol>
  <li>
  Is that the right place to bring in a module?
  Is there an accepted way of checking the module was loaded and handling it?</li>
  </ol>
  
  <p>2.
  Is that the most efficient way of loading just the method I want? </p>
  
  <p>3.
  Overall really, is that the way you would solve this particular problem?</p>
  
  <p>I guess I am also hunting for a steer vs your 'real world' expyrience.  Is the string module so common that you'd more than likely have it loaded anyways?</p>
  
  <p>If that is the case, how do you check a module is loaded?</p>
  
  <p>Would it be rather pointless using exceptions to handle the string module not being findable because by default string is always going to be available?</p>
  
  <p>(ps taken from <a href="http://openbookproject.net/thinkcs/python/english2e/ch07.html" rel="nofollow">Learning With Python</a> ftw)</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>OK, for those wondering, as has been pointed out - my question is misguided because I failed to understand that a string obj in Python already contains the replace() method.</p>
  
  <p>This was caused because I failed to do something like:</p>
  
  <pre><code>&gt;&gt;&gt; dir("a string")
  </code></pre>
  
  <p>which would have revealed all of the methods, including 'replace':</p>
  
  <p>So then I could have followed that up by typing:</p>
  
  <pre><code>&gt;&gt;&gt; "a string".replace.__doc__
  </code></pre>
  
  <p>Which would have spewed out:</p>
  
  <blockquote>
    <p>'S.replace(old, new[, count]) ->
    string Return a copy of string S with
    all occurrences of substring old
    replaced by new.  If the optional
    argument count is given, only the
    first count occurrences are replaced.'</p>
  </blockquote>
  
  <p>Now of course I knew this replace() method MUST exist somewhere, but I did not yet know how to look and discover for myself - the Python docs being frankly far too "loosely coupled" for me. Thanks for the answers - but am posting this EDIT in case any other similarly ignorant and bemused but willing Python advocate should stumble by.</p>