question about modules

Jeffrey Maitland maitj at vianet.ca
Fri Dec 10 14:15:31 EST 2004


Jon writes: 

> Hi, 
> 
> The following four lines of code: 
> 
> import sys, os, re
> sentence = raw_input("Enter a sentence:  ")
> capwords (sentence)
> print sentence 
> 
> gives me the following error:  NameError: name 'capwords' is not defined 
> 
> As far as I can tell from the online docs, "capwords" should be defined in
> the built-in "regex" module.  Why is it telling me that capwords is not
> defined? 
> 
> I am completely new to Python so my apologies for such a basic question! 
> 
> Thanks,
> Jon 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Hello Jon, 

The reason for that is you only imported the module(class/object) now to use 
its methodes you need to call the object. 

in this case to fix the problem you are having you would have to do it 
either one of these methodes (unless there are more that I am not aware of)
1/
import sys, os, re
sentence = raw_input("Enter a sentence:  ")
re.capwords (sentence) # <------ notice the re.capwords this
                      # calling the capword method of the re module.
print sentence 

2/
import sys, os
from re import * # <---- import all methods of re and allow them to be
                #       used locally
sentence = raw_input("Enter a sentence:  ")
capwords (sentence)
print sentence
# this imports all the methodes in the re module so they can be used
# localy. 

or
import sys, os
from re import capwords  # <---- import only capwords method of re
sentence = raw_input("Enter a sentence:  ")
capwords (sentence)
print sentence
# this import only imports the capwords methode of the re module 


I hope this helps you some and if i used the incorrect terminology I am 
sorry and I hope someone points it out to me. 

Jeff Maitland



More information about the Python-list mailing list