Hi list ! I want to know whats so great in OOP... I have learned some of it, but I don't understand why everybody like it so much... Can anyone give me an example for a task that could be done only with OOP or will be much simpler to do with it ?
Thanks in advance.
Hi Mark and all,
I've fairly recently began to use OOP myself.
In addition to the organization and reusability mentioned by Kent and Bill, I'd mention the instance namespace and subclass specialization.
1) Namespace issues With procedural (or imperative -- don't know which is the right terms for non-OOP code which employs functions) code, you can have issues caused by namespaces. Just yesterday, someone on the main python list/newsgroup had code something like:
.>> a = 1 .>> b = 2 .>> def silly(): ... if a: ... b = b + 1 ... .>> silly() Traceback (most recent call last): File "<interactive input>", line 1, in ? File "<interactive input>", line 3, in silly UnboundLocalError: local variable 'b' referenced before assignment .>>
This raises an exception, because the b = b + 1 line in silly() is interpreted as saying "OK, create a function local name and assign it the result of adding 1 to the function local name b -- hey wait, that doesn't exist yet! BARF."
A class based approach works fine:
.>> class Silly_Test: ... def __init__(self): ... self.a = 1 ... self.b = 2 ... def silly(self): ... if self.a: ... self.b = self.b + 1 ... print self.b ... .>> st = Silly_Test() .>> st.silly() 3
Similiarly, if you have functions with function local names that call other functions, without classes you often need either 1) to make the names global (and thus have less transparent code) or 2) to pass objects as arguments and return them as values (and thus have ugly noisy code) just so all of the functions can `see' all of the values that they need to know about. With classes, a few selfs but you an instance namespace which makes the issue go away. (The `selfs' felt like noisy clutter to me at first, but I soon got used to them.)
2) Specialization
With a lot of help from this list, the project I recently used to get the OOP groove was a toolkit for processing files from a freeware/shareware program I use a lot. The app is a tree-style folding notebook or outliner, and every tree node has an article. Those articles might be in text, RTF, or html. I made a Node class and three subclasses Text_Node, RTF_Node and HTML_Node. When parsing a file, I then create an instance of the appropriate Node class to store a node's worth of data.
This pays off when doing things which differently formatted nodes need to do differently. For instance, the application supports internal links between nodes on its tree. The links in the html formatted nodes are like html links, those in the text nodes are in a simple linking syntax the developer of the app came up with. Giving each node subclass its own create link method:
def create_link(self, target): # logic to make and return a link to target goes here
means that I can `tell' a node instance to create a link and trust it to make the sort of link appropriate to its own article format. With a procedural approach, there would have to be something more like:
def create_link(current_node, target): current_node_format = get_node_format(current_node) if current_node_format = 'Text': return create_text_link(target) if current_node_format = 'HTML': return create_html_link(target) # and so forth
A while back, on this list, Bob Gailer wrote:
Whenever you find yourself writing an if statement ask whether this would be better handled by subclasses.
I didn't get it at first, but what Bob meant were the sorts of `ifs' in def create_link(current_node, target). It turns out these are wise words.
Hope there is some help in all these words :-)
Brian vdB
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor