Simple Class Question - need clarity please

Alan Gauld alan.gauld at btinternet.com
Fri Apr 9 19:42:47 EDT 2004


On Sat, 10 Apr 2004 00:03:07 +0100, "Stevie_mac"
<no.email at please.com> wrote:

> What is wrong with this?...
> 
> import win32con, win32ui
> from pywin.mfc import dialog #, window
> class Window:
>     def MakeDlgTemplate(self):
...
>     def DoModal(self):
>         mw = win32ui.CreateDialogIndirect( self.MakeDlgTemplate() )
>         mw.DoModal()
> 
> # file test.py #
> import mywindow
> class MyClass(mywindow.Window):
>     def Show(self):
>         self.DoModal()
> 
> t = MyClass()
> t.Show()
> 
> . . . I get
> NameError: global name 'MakeDlgTemplate' is not defined  ???

Its a good idea to post the entire error traceback so we see the
actual line of code the error refers to. This error usually means
you accessed a name from a module without prefixing it with the
module name. But so far as I can see you haven't done that here.

> another 1 I managed to clear up was . . .
> TypeError: Show() takes no arguments (1 given)
> . . . until I added 'self' to the Show function  ???

Yes, every method of a class must have a parameter that refers to
the instance at call time, usually called self (or maybe "this"
if you come from Java or C++)

At runtime Python effectively calls

class.method(instance)

> I'm also struggling with other things like...
> Where do I save .py files?

Anywhere you like! I have a folder called Projects\Python where I
keep mine :-)

So long as Python knows where to find them - either by modifying
the sys.path value (explicitly or via the registry setting) or
the PYTHONPATH environment variable...

> Will 'import' find my files where ever they are? 

Yes if one of the two locations above are set.

HTH,

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Python-list mailing list