[Tutor] When to use a class
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, rSOFTWARE\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 signature.asc Description: Digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When to use a class
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 [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. 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, rSOFTWARE\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@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When to use a class
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, rSOFTWARE\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
Re: [Tutor] When to use a class
Eric Lake 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. My take on that question is here: http://personalpages.tds.net/~kent37/stories/00014.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor