[Tutor] When to use a class

Michael Langford mlangford.cs03 at gtalumni.org
Mon Sep 17 19:32:41 CEST 2007


Short sections of code are not where classes shine.

Classes become much more valuable when you start to get a lot of hairy
details you need to pass around. For your code, for instance, you could pass
in the whole registry key you want, and have out pop a RegKey object.

This would be say, usable in a system where you needed to do several
operations on this object. If you were accessing multiple registry keys for
instance, it would probably be nice to make a RegKey class that could be
created with a handy method. If you were only accessing one key one time, a
simple routine will serve you well.

Realms where classes are almost always used
Simulations, GUI elements, business systems of any size, many parsers,
especially XML and HTML parsers.

Realms where they're used much less:
Basic text processing, system administration tasks, simple database systems,
and number crunching apps.

       --Michael


-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com

On 9/17/07, Eric Lake <ericlake at ubuntu.com> wrote:
>
> I am still trying to understand when to use a class and when not to. All
> of the coding that I have done in the past (Python, Perl) has been
> procedural / functional. I would really like to do more OOP but I am not
> really sure when I need it.
>
> I have the following code. Is there any way that it would benefit from
> using a class?
>
> <code>
>
> #!/usr/bin/env python
>
> import string
> import _winreg
> import sys
>
> compName = sys.argv[1]
>
> x = _winreg.ConnectRegistry(compName,_winreg.HKEY_LOCAL_MACHINE)
> y = _winreg.OpenKey(x,
> r"SOFTWARE\Intel\LANDesk\VirusProtect6\CurrentVersion")
> avParent = _winreg.QueryValueEx(y,"Parent")[0]
>
> _winreg.CloseKey(y)
>
> print "Computer: %s \tAV Parent: %s" % (compName,avParent)
>
> </code>
>
>
> --
>
> Thanks
> Eric Lake
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iQEVAwUBRu6u2ZLZLpR+JU3MAQp0Dgf/cMXUpmBnVM3NPQu6b2LVwEN/L5+DG0hn
> r3oyyVr56EIz04zl6fRqOk4NPkW0d0y5x2uvwWMCgvy64gyd9cHSrwCPxorCcf1j
> /71QhXA0Nx44mwJK6ahCatcfimzUF1MeykOX0oxcaAP26JDtV7eF0jYjzizsEzmE
> Q+2JlWzlOKrljxKL1zJLPepzubwoWFIYFmlXfYdbk2HkMCPmzPfAipEZW8WPj5xU
> Fu1lGWEuODSEn/+d4X6tPNlJLOAxgL01IPPUZZSso6gfjlLDHYVPTYTEUDgZIrLD
> XPuFpNT7tT8jQWZKg6OFjFS2P6/LVc02AYskXjegmEyMfNDZ27qLMw==
> =9N/n
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> 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/20070917/b615fbb0/attachment.htm 


More information about the Tutor mailing list