question about modules
Jon
jon.f.crawford at gmail.com
Fri Dec 10 14:39:54 EST 2004
Hi Jeff,
That makes sense -- thanks. However now when I use "re.capwords (sentence)"
I get a different error message:
AttributeError: 'module' object has no attribute 'capwords'
Each of the other two suggested implimentations produce a similar error
message. Is there something even more basic that I am failing to do? I'm
using the IDLE GUI in WinXP, Python release 2.4...
Thanks!
Jon
"Jeffrey Maitland" <maitj at vianet.ca> wrote in message
news:mailman.7481.1102706134.5135.python-list at python.org...
> 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