[Tutor] Syntax when using classes question.

Chris Hengge pyro9219 at gmail.com
Sun Nov 12 00:34:16 CET 2006


I guess I'm just lost as to the point of classes... Outside of using them to
abstract a collection of methods that have similair roles I dont see the
point.. But when I group similar methods together (promptForge is a bad
example, but I've got class fileForge which has a few file writing or
reading methods), people tell me its overkill (basicaly) =P

On 11/11/06, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> Chris,
>
> >    pyForge.promptForge.prompt()
> > TypeError: unbound method prompt() must be called with promptForge
> > instance
> > as first argument (got nothing instead)
> >
> > ^ Thats why... I have to use :
> > import pyForge
> > pyForge.promptForge().prompt()
>
> I think the real issue that Luke was raising was why you are using
> that style.
> Basically you are creating an instance of pyForge just to call the
> method
> then it gets deleted again (by garbage collection). Thats a very
> inefficient
> function call!
>
> The normal usage of classes and objects is that you create an
> instance and retain it, thus:
>
> myForge = pyForge.promptForge()
> myForge.prompt()
> ...
> # and use it again
> myForge.prompt()
>
> Thus the myForge instance is retained until you are finished using
> any of the methods.
>
> The reason for that is that the instance will hold the data used
> by the methods and so if you delete it you delete the data too!
> For examplein your case you could have promptForge hold the
> prompt message you want to display. That could nbe initialised
> in the constructor, thus you could have several promptForge
> instances each with a different prompt message, lie this:
>
> class promptForge:
>     def __init__(self.,msg = "? "):
>        self.message = msg
>        self.value = None  # stores last input value
>    def prompt(self):
>         print self.message,
>         self.value = raw_input()
>         return self.value
>
> And use it thus:
>
> yes_no = promptForge("Continue? [Y/N] ")
> quit = promptForge("Hit Y to quit")
> intValue = promptForge("Enter an integer between 0 and 9")
>
> if yes_no.prompt() in "yY":
>     for n in range(3):
>         x = int(intValue.prompt())
>         print x
>         if quit.prompt() in "yY2: break
>
> But if you don't have any internal data, you don;t really
> need instances...
>
> Now it looks like you have created some classes with no
> shared data just functions(methods). In that case you would
> be better just using functions inside the module. Its easier to
> use and also has a lower resource usage and potentially
> lower call overhead (no method lookup/dispatch)
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061111/5040e0e5/attachment.html 


More information about the Tutor mailing list