"Eric Lake" <[EMAIL PROTECTED]> 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.

You virtually never * need* it. Sometimes it makes coding simler,
but you can always do without.

OOP is a different way of approaching programming, it requires
a different way of thinking about your program structure. Thats why
established programmers tend to find it much harder to adopt OOP
than beginners with no prior experience!

> I have the following code. Is there any way that it would benefit 
> from
> using a class?

No, the code is too short, it is comparable to
a method within a class. If you are mainly writing short snippets
then its likely OOP will be overkill.

If you did create a class then the registry might be a candidate.
You might want to build a registry object woith friendlier method
names than those exposed by the module. But for something
this short there is no real advantage.

Remember that objects are things. If you have a thing in your program
then there is a possioble lass there. The actions you perform on
that thing could be methods of the class. Some OOP gurus don't
like the noun/verb approach but franlly I still find it the best 
starting
point for people who are learning OOP. Write down a description
of your program in English, underline the nouns and categorise
them - people, places etc. The categories are potential classes,
the instances are potential objects. Now look at the verbs associated
with the objects you identified. These are potential operations of
the classes. If there are no operations discount the class!

In your example there is a computer and a registry.
But there is nothing done to the computer, it is only
a parameter to the registry, so discount it.
The registry object is connected and queried.

So you could write:

class registry:
    def __init__(self, computer, key=None): ...
    def queryKey(key=None):...


But I repeat, in your case the overhead of writing all the
class code is bigger than your snuippet, so is only
worth while if you would be reusing the registry object,
either in the same program or in others that you write.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


<code>

#!/usr/bin/env python

import string

You probably don;t need this, string module is pretty much
redundant nowadays.

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> 


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to