Thursday, 23 January 2014

Week3 - Objected-Orientated Programming (OPP)


Hi Everyone!

This is  Azura. I am a first year math student in U of T, taking CSC148 this term. This is my first time writing a blog, and it's even in English! There may be some language errors, so please free to point it out~~

Besides CSC108, I have no programming experience before, when Danny's talking about 148 will focus on object-orientated programming, I have no idea what's going on. I don't know what does it mean by that, but the person sits besides me seems like she knows everything! 

What's the difference between the program I wrote in 108 and the program that I am going to face in this class?? I go back and try to read those supplemental pages on our website. Those kinds of programming we did in CSC108 are considered as procedural programming in formal definition, it is about writing functions that operate on data. 148 is about object-oriented programming, it is focus on the creation of objects contains functions and datas together. 

Class is analogous to a "factory" to produce instances and also "a template for the objects that are instances of it" on the website. I then more understand what a class is and why we need to use class. Then when it comes to my lab session, when I am actually doing it, I am more familiar with the way I learned about how to handle a object-orientated project. I worked with my partner and we think of how handle the whole object at first , then we focus on doing every small parts and finally review and check the whole object. I am more confident now, and know the algorithm to construct a class.

The python style we learned in 148 is different from CSC108, so I was also confused about the different ways of constructing a class. Then I found examples from both classes and compare both, I think the thinking to construct a class is the same, the difference is the writing style. 

The similarity that I figure out is:
1. Define a __init__ function at first to initialize the class
2. Every function needs a self default parameter
3. Can use built-in functions to help out

 The difference that I figure out is:
1. A class can also has a description which right below the class header and above the __init__ function
2. Contract type goes right after the parameter, whereas it was wrote independently below function header. 
3. Besides those built-in functions, we can also use list comprehensions with the following format [(expression) for (name) in  (iterable)], here () indicates the variable that we can change to make our desired comprehension.
Also, there is another very useful list comprehension, especially when we write our return statement. Instead of write 
if a > b:
return "Hi"
else:
return "Hello"
we can write 
return "Hi" if a > b else "Hello"


This week's lecture also talks about recursion and inheritance, which can also be used to create a class. I will talk about these two terms next week. :)

3 comments:

  1. note it seems some version of Python3 ask to add () to specify the output when using print and return.Any way, good writing.

    ReplyDelete
    Replies
    1. Thank you!! This is very important!
      Without () when using print in Python3 will cause a SyntaxError

      Delete
  2. Good organization! Seems you have a thorough understanding about Objected-Orientated Programming.

    ReplyDelete