I am working my way through some samples, and came up with this working answer – the challenge being set in the doctests:
def remove(sub, s):
"""
>>> remove('an', 'banana')
'bana'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Missippi'
>>> remove('egg', 'bicycle')
'bicycle'
"""
if sub not in s: return s # 1
from string import replace # 2
return replace( s, sub, '', 1) # 3
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.
On my line:
-
Is that the right place to bring in a module?
Is there an accepted way of checking the module was loaded and handling it?
2.
Is that the most efficient way of loading just the method I want?
3.
Overall really, is that the way you would solve this particular problem?
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?
If that is the case, how do you check a module is loaded?
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?
(ps taken from Learning With Python ftw)
EDIT
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.
This was caused because I failed to do something like:
>>> dir("a string")
which would have revealed all of the methods, including ‘replace’:
So then I could have followed that up by typing:
>>> "a string".replace.__doc__
Which would have spewed out:
‘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.’
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.
By convention, imports are placed at the top of your module/script. It makes them easier to find for you and others reading your code. There are sometimes good reasons to put imports elsewhere, say a conditional import with significant overhead.
Another convention is not to ‘check’ that a module is loaded but instead to rely on the Python interpreter to raise a
NameErrorexception if the object you are dereferencing is not available. Also note that multiple imports of the same module are safe as the import mechanism checks if it has been imported already.You may want to read http://docs.python.org/tutorial/modules.html and http://diveintopython.net/object_oriented_framework/importing_modules.html
In this particular case, you don’t need to import anything as
.replace()is a method of the string object. Thestringmodule is largely redundant (not entirely, but it is imported rarely in my experience).