Sunday, January 19, 2014

Object-Oriented Programming

When I first started CSC148, object-oriented programming was a familiar concept with an unfamiliar name, and I had some trouble putting the two together. The prerequisite CSC108 class taught me the foundation for the new material in this course, but I came out of CSC108 not completely understanding Classes.

Primarily, I didn't comprehend how the __init__ function worked, and especially how to set up attributes. I didn't know what self.x = x meant, or that it was possible to set up attributes without making them parameters in the constructor function.
To sum it up, I panicked a little when Danny pulled up Classes in our first lecture.

Since then, I've learned that __init__ basically sets up the class object, and gives it the "default settings" - the attributes. self.x = x creates instance variables for the object, and the __init__ function can even be omitted if it had no attributes (a surprising fact I learned in Exercise 1).

What really helped me to get a grasp of Classes was reading How to Think Like a Computer Scientist: Learning with Python. The section on Classes and Objects was clear and descriptive, and the way it was written was easy to understand. A particular explanation I thought was interesting was:

" The original syntax for a function call, print_time(current_time), suggests that the function is the active agent. It says something like, “Hey, print_time! Here’s an object for you to print.”
In object-oriented programming, the objects are considered the active agents. An invocation like current_time.print_time() says “Hey current_time! Please print yourself!”"


No comments:

Post a Comment