[Tutor] My horrible looking program needs improvement.
W W
srilyk at gmail.com
Mon Nov 24 19:21:44 CET 2008
On Mon, Nov 24, 2008 at 10:00 AM, Peter van der Does <peter at avirtualhome.com
> wrote:
> I wrote my 1st Python program and it works as I want it to work but
> ,IMHO, it looks horrible.
Nothing to be ashamed of, unless you don't mind it looking horrible. The
fact that you want a clean program is a good sign!
> <snip>
> Why I think it looks horrible:
> Everything is in main(), I would like to use functions but ran into
> problems with the scope of my variables.
> The CLI arguments check is just a bunch of IF's
You would probably benefit from using a class then (especially if you have
worries about your "main" program looking clean).
You can create global class variables that each of the methods/functions
have access to with the self. - for instance
In [11]: class IP:
....: address = "192.168.1.1"
....: def printAddress(self):
....: print self.address
....:
....:
In [12]: x = IP()
In [13]: x.printAddress()
192.168.1.1
---
Then you just declare a new instance of your class (see In[12]) and you have
access to all its methods and variables;
In [15]: x.address = "127.0.0.1"
In [16]: x.printAddress()
127.0.0.1
> Is this the place where somebody could go over my program and give
> some pointers on how to improve the structure or is there a forum out
> on the Net that can help me out.
I don't know about anyone else, but I suspect most people are at least as
busy as myself, but perfectly willing to help answer your questions, but I
doubt anyone has the time (even if they want) to re-write your program /for/
you, but we'll be glad to help if you get stuck on a problem (or even need
pointers on where else to look, if Google or your favourite search engine
fails to turn up any information)
Using a class should really help clean up your program and help eliminate
your scope problems. Here's a nifty little tutorial on python classes:
http://www.diveintopython.org/object_oriented_framework/defining_classes.html
Give classes a try and see if helps. If you're still having issues or you
don't understand something, feel free to ask!
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081124/d2f26020/attachment.htm>
More information about the Tutor
mailing list