Friday, 3 April 2015

Revisit of OOP Concepts




 I will look back at my Summary of OOP Concepts post.

I believe I have covered objects and inheritance well. If there is anything to be added, then it should be a brief note distinguishing functions and methods.

Check out the code I briefly wrote:

class Happy:
    ''' insert description '''
    def __init__(self):
       
        self.things_that_make_me_happy = ['coconuts','creamcheese','tea']
       
    def delete(self, thing):
       
        if thing in self.things_that_make_me_happy:
            self.things_that_make_me_happy.remove(thing)
        else:
            return "This thing did not make you happy!"
       

def delete(lst, thing):
   
    if thing in lst:
        lst.remove(thing)
    else:
        return " item not in list."
   

In the shell:

>>> d = Happy()
>>> d.delete('tea')
>>> d.things_that_make_me_happy
['coconuts', 'creamcheese']
>>> delete(d.things_that_make_me_happy, 'coconuts')
>>> d.things_that_make_me_happy
['creamcheese']


So is there really a difference between the functionality of delete and the delete method in Happy?
Not really.
The only difference(quite negligible here) is that the delete method must be activated(used) in objects, not just on any list that we randomly create.
In other words, the OBJECT has to exist for the utilization of the method to occur.
However, function delete is global. The lst does not have to be the list from the object Happy.

A further note:

After a semester of programming using OOP concepts, I must emphasize again that OOP is a very efficient way of programming! As a student, my mind set has shifted from procedural to objected-oriented. Whenever I start to program, I think about how I can organize the features, key aspects, and data(if any) in objects for purpose of efficiency. Combined with recursion, as we have seen in A2 and A3, we are able to write larger programs with less number of lines than we have used in 108.


---------------------------EXOCOETIDAEE-------------------------------


No comments:

Post a Comment