Need Help

Piet van Oostrum piet at cs.uu.nl
Mon May 25 10:09:41 EDT 2009


>>>>> abosalim <mohammed.abosalim at gmail.com> (a) wrote:

>a> I modified the method,but it can't identified it.(self.res=textcorrect
>a> (self.string)
>a> NameError: global name 'textcorrect' is not defined)
>a> This is what i done:

[...]
>a> class Ex3:
[...]
>a>     def textcorrect(str):
>a>         s=[]
>a>         for i in tokenize.whitespace(str):
>a>             s.append(correct(i))
>a>             result= " ".join(s)
>a>             return result
>a>     def new(self):
>a>         self.string = self.contents.get()
>a>         #pass contents of textfield to textcorrect()method above
>a>         self.res=textcorrect(self.string)
>a>         self.frame2 = Frame()
>a>         self.frame2.pack()
>a>         self.words = Label(self.frame2, text=self.res, fg="blue", font=
>a> ("Arial", 16))
>a>         self.words.pack()

So now you have put textcorrect inside the class Ex3. But that means
that it is an instance method now. So you need to call 
     self.textcorrect(self.string) 
otherwise it is not known. But then you must also give it a self
parameter. The same applies to the other methods, so in textcorrect you
have to call self.correct(i)  and give correct a self parameter. etc.

Or you make them all static methods and use Ex3.textcorrect().

I don't think putting all the methods in a class is a good idea, as you
only need one instance of it and most methods are static-like anyway.
Putting it in a class is a Java-ism. A module for this is more Pythonic.

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list