Re: ****SPAM(11.2)**** [Tutor] Larger program organization
At 03:04 PM 2/16/2005, Brian van den Broek wrote: Terry Carroll said unto the world upon 2005-02-16 16:18: On Fri, 11 Feb 2005, Bob Gailer wrote: Whenever you find yourself writing an if statement ask whether this would be better handled by subclasses. Whenever you find yourself about to write a global statement, consider making the variables properties of a class. Bob -- Brian already asked for an explanation of your first statement, and I found the ensuing discussion very instructive. Can you explain the second? As an aesthetic point, I hate globals, and I'd love a discussion with some examples of using class variables as a way of avoiding this. Terry Brian's response is similar to what I'd say. Hi Terry and all, I'm probably not the best person to explain this, but I've got a use case that might help illustrate. The thing that finally got me to write my first Class statement was a procedural program where I had a hard to find bug. I wrote a debug_report function which would print out an informative report about the state of all the objects I suspected were implicated in the bug. Then, I put debug_report calls at the suspicious places. But, my procedural code had function call chains 4 or 5 links deep, and at no level did any of the functions have access to all of the objects of interest. To make it work in purely procedural, I had to either make many objects global or litter my functions with passing objects up and down as parameters that weren't needed for the function's tasks, but simply so debug_report could see them. I made a class, put my functions in it, and suddenly, they all could `see' the objects of interest without the passing and returning of objects just for the sake of visibility. Some `self's sprinkled around did the work instead. So, even if you don't make use of further OOP features (as I am learning to do on other threads), classes give you a namespace option between globals and locals -- `continentals' if you like ;-) HTH, and looking forward to more expert explanations, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Bob Gailer mailto:[EMAIL PROTECTED] 303 442 2625 home 720 938 2625 cell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(11.2)**** [Tutor] Larger program organization
Terry Carroll said unto the world upon 2005-02-16 16:18: On Fri, 11 Feb 2005, Bob Gailer wrote: Whenever you find yourself writing an if statement ask whether this would be better handled by subclasses. Whenever you find yourself about to write a global statement, consider making the variables properties of a class. Bob -- Brian already asked for an explanation of your first statement, and I found the ensuing discussion very instructive. Can you explain the second? As an aesthetic point, I hate globals, and I'd love a discussion with some examples of using class variables as a way of avoiding this. Terry Hi Terry and all, I'm probably not the best person to explain this, but I've got a use case that might help illustrate. The thing that finally got me to write my first Class statement was a procedural program where I had a hard to find bug. I wrote a debug_report function which would print out an informative report about the state of all the objects I suspected were implicated in the bug. Then, I put debug_report calls at the suspicious places. But, my procedural code had function call chains 4 or 5 links deep, and at no level did any of the functions have access to all of the objects of interest. To make it work in purely procedural, I had to either make many objects global or litter my functions with passing objects up and down as parameters that weren't needed for the function's tasks, but simply so debug_report could see them. I made a class, put my functions in it, and suddenly, they all could `see' the objects of interest without the passing and returning of objects just for the sake of visibility. Some `self's sprinkled around did the work instead. So, even if you don't make use of further OOP features (as I am learning to do on other threads), classes give you a namespace option between globals and locals -- `continentals' if you like ;-) HTH, and looking forward to more expert explanations, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(11.2)**** [Tutor] Larger program organization
On Fri, 11 Feb 2005, Bob Gailer wrote: > Whenever you find yourself writing an if statement ask whether this > would be better handled by subclasses. Whenever you find yourself about > to write a global statement, consider making the variables properties of > a class. Bob -- Brian already asked for an explanation of your first statement, and I found the ensuing discussion very instructive. Can you explain the second? As an aesthetic point, I hate globals, and I'd love a discussion with some examples of using class variables as a way of avoiding this. Terry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(11.2)**** [Tutor] Larger program organization
Bob Gailer said unto the world upon 2005-02-13 10:13: At 03:21 PM 2/12/2005, Brian van den Broek wrote: [snip] > I am curious about Bob's "Whenever you find yourself writing > an if statement ask whether this would be better handled by subclasses." Thanks Bob and Alan, It's quite clear now :-) Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(7.4)**** Re: ****SPAM(11.2)**** [Tutor] Larger program organization
Bob Gailer wrote: At 03:21 PM 2/12/2005, Brian van den Broek wrote: [snip] > I am curious about Bob's "Whenever you find yourself writing > an if statement ask whether this would be better handled by subclasses." class A: ... class A1(A); def foo(self, ...): statements to process object of type 1 class A2(A); def foo(self, ...): statements to process object of type 2 That takes less code. Eliminates the type property. I get greater visibility about the existence and distinction of the two (or more) sub-types. I now can much more easily extend each subtype. Also you can more easily *add* a new subtype. Using if statements, if you add a new type you have to find all the relevant conditionals and add another condition. With subclassing, you create a new subclass and define the necessary methods. The changes are localized to the subclass and much easier to figure out. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(7.4)**** Re: ****SPAM(11.2)**** [Tutor] Larger program organization
At 03:21 PM 2/12/2005, Brian van den Broek wrote: [snip] > I am curious about Bob's "Whenever you find yourself writing > an if statement ask whether this would be better handled by subclasses." I start out writing a class like: class A: def __init__(self, type): self.type = type ... def foo(self, ...): if self.type = 1: statements to process object of type 1 else: statements to process object of type 2 The '"if statement" alerts me to consider creating subclasses for types 1 and 2: class A: ... class A1(A); def foo(self, ...): statements to process object of type 1 class A2(A); def foo(self, ...): statements to process object of type 2 That takes less code. Eliminates the type property. I get greater visibility about the existence and distinction of the two (or more) sub-types. I now can much more easily extend each subtype. Bob Gailer mailto:[EMAIL PROTECTED] 303 442 2625 home 720 938 2625 cell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(11.2)**** [Tutor] Larger program organization
> I am curious about Bob's "Whenever you find yourself writing an if > statement ask whether this would be better handled by subclasses." > > Could you explain a bit more? One of the basic purposes of OOP is to eliminate if/switch statements that are conditional on the type of the object being handled. So when writing OO code every time you see an if statement you should ask is this really part of the logic or am I habdling a special type of processing that should be in a separate subclass. For example, if you were writing your Node class parsing method and you started writing code like: if dataType == 'Some literal value' # extract one format of data else # extract another data format Then maybe you should have two classes and rely on polymorphism to do the two different types of extraction. Thus subType = DataType() subType.extractData() By eliminating the if/else you potentially eliminate an ongoing maintenance headache and this is one of the big wins of OOP. HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(11.2)**** [Tutor] Larger program organization
Bob Gailer said unto the world upon 2005-02-11 15:34: At 10:39 AM 2/11/2005, Ryan Davis wrote: I'm starting to make a code-generation suite in python, customized to the way we ASP.NET at my company, and I'm having some trouble finding a good way to organize all the code. My take on doing that in Python: Organize things into modules. Especially with an eye to potential reuse. Look at the module index in the docs to see how most of the "standard" modules focus on doing one thing well. Within a module create classes for every conceivable object. Whenever you find yourself writing an if statement ask whether this would be better handled by subclasses. Whenever you find yourself about to write a global statement, consider making the variables properties of a class. Hi all, as readers of another concurrent thread will know, I am, with lots of help, just beginning to start thinking about proper OOP design. I am curious about Bob's "Whenever you find yourself writing an if statement ask whether this would be better handled by subclasses." Could you explain a bit more? Thanks and best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(11.2)**** [Tutor] Larger program organization
> >way we ASP.NET at my company, and I'm having some trouble finding a good > >way to organize all the code. > > My take on doing that in Python: > > Organize things into modules. Especially with an eye to potential reuse. > Look at the module index in the docs to see how most of the "standard" > modules focus on doing one thing well. Hi Ryan, And just making sure; are you using packages? http://www.python.org/doc/tut/node8.html#SECTION00840 Most large Python programs that I've seen will first partition their functionality into modules, and if that's not enough, then they further break themselves down into packages. Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: ****SPAM(11.2)**** [Tutor] Larger program organization
At 10:39 AM 2/11/2005, Ryan Davis wrote: I'm starting to make a code-generation suite in python, customized to the way we ASP.NET at my company, and I'm having some trouble finding a good way to organize all the code. My take on doing that in Python: Organize things into modules. Especially with an eye to potential reuse. Look at the module index in the docs to see how most of the "standard" modules focus on doing one thing well. Within a module create classes for every conceivable object. Whenever you find yourself writing an if statement ask whether this would be better handled by subclasses. Whenever you find yourself about to write a global statement, consider making the variables properties of a class. Within classes create methods applicable to those classes. Within subclasses create methods applicable to those subclasses. I keep writing it, but it feels more and more spaghetti-ish every day. I'm going to look at the other stuff in site-packages to see if I can glean any wisdom, and have googled a bit, coming up mostly blank or with trivial examples. Are there any helpful links or advice anyone has for building larger systems? My background is mostly C#, so I'm used to the ridiculous rigidity of strongly-typed languages. I have been using python for helper apps for a few months now, so am pretty familiar with the syntax now, but I don't know any of the patterns yet. My nefarious goal is to supplant C#/ASP.NET with Python, but I need to figure out how to make programs clients want to pay for before I can make any reasonable argument. Thanks, Ryan Ryan Davis Director of Programming Services http://www.acceleration.net/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Bob Gailer mailto:[EMAIL PROTECTED] 303 442 2625 home 720 938 2625 cell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor