In labs this week, we had to use recursion on computational problems. At first, the problems were difficult (possibly due to me skipping class this week). Eventually the lab was solved, though our solutions were not the most efficient. Instead of confining all of our code to the return statement and using built in methods such as 'is' and 'isinstance', like in the examples done in class, we coded it the previous way and had to futz with it for quite a while before it started working. We couldn't figure out why the program wouldn't stop once it got the correct value, but would keep recurring and sometimes wouldn't return what we were looking for. After getting home from the lab I took a second look at the class slides and examples, and quickly figured out how to do it with only using code within the return statement (though getting used to the syntax did take a moment). Now, even though the program would keep recurring even after getting the correct value, all return values were stored and the max return value was the one printed. Since max(True, False) = True, putting the code within a max(...) statement would always return the correct statement, since just needed to know if at least one iteration of the function would return True. I'm glad I understand this better now, and will be trying to code everything in this more efficient way from now on.
Thursday, February 6, 2014
Wednesday, January 22, 2014
Reflecting on Object Oriented Programming
In class we have been working on object oriented programming. I remember when we started this at the end of csc108, I was confused by it (especially with why some methods had underscores surrounding them, what does it mean??) but now I feel I have a good grasp on the technique, and understand the purpose of the __init__ method (among others). Something that does still bother me that I noticed while doing last weeks Exercise is that, if you have a method, 'hop', within class 'Bunny', and you let a = Bunny(), and you call a.hop(), everything is fine. But, if you say something like, a.hop = 'hop', and then try to call a.hop() again, you get an error! It seems like this is because python now associates a.hop with the string 'hop', so when you call a.hop() again, it is substituting 'hop' in, so it thinks you're trying to call a string as a function. However, if you call Bunny.hop(a), it will still work. So, I guess I understand (vaguely) why this happens, but I still find it a bit irritating and inconsistent.
Also, I don't understand the benefit of writing our function definitions as something like, "def hop(self: 'Bunny') -> None", especially since python red-underlines the "-> None" part. Why are we writing it like this? Me and python agree that it is silly, though I can see how it would be useful if you want to give a variable in the function a set value (such as "def hop(self: 'Bunny', jump = 'yes') ... print(jump) " would print 'yes' if a.hop() was called, or you could say a.hop('no') and it would print 'no'). Anyway, those are my thoughts for the week, so long!
Also, I don't understand the benefit of writing our function definitions as something like, "def hop(self: 'Bunny') -> None", especially since python red-underlines the "-> None" part. Why are we writing it like this? Me and python agree that it is silly, though I can see how it would be useful if you want to give a variable in the function a set value (such as "def hop(self: 'Bunny', jump = 'yes') ... print(jump) " would print 'yes' if a.hop() was called, or you could say a.hop('no') and it would print 'no'). Anyway, those are my thoughts for the week, so long!
Subscribe to:
Posts (Atom)