For this week, i'd like to indicate that i'd want my Object Oriented Programming SLOG that i've previously written to be marked which is linked here.
In addition to that post, i'd like to add more details and go into depth into the concept of OOP. I've gotten positive feedback that my post was very informative and useful but i'd like to demonstrate these definitions into examples that we have come across throughout the course to get an idea of what it's really talking about and to help those who learn through examples instead of merely memorizing the definition, I know I do.
I will show all of these examples based off of our assignment with Subtract Square, as you all know was our Assignment 1 which was a very good exercise to practice these new concepts in OOP.
So firstly, in Subtract Square, we have encountered many Classes which is defined in my previous post.
Examples of Classes in our assignment are shown in the General Classes that we had to make for the game followed by its docstring that informs us what types, methods..etc are in this particular class, such as:
class GameView:
'''
A game view for a two-player, sequential move, zero-
sum, perfect-information game.
'''
class Strategy:
'''Interface to suggest moves for a GameState.
Must be subclassed to a concrete strategy. Our
intention is to provide a uniform interface for
functions that suggest moves.
'''
class GameState:
'''(Insert docstring of all the methods in this class...
you get the idea)
'''
As stated in the previous post, these classes contain methods and also __init__, __str___, __repr__ etc and whatever relevant methods the programmer wishes to include into this class.
The second thing we've encountered in Subtract Square that is an OOP is Inheritance (Refer to previous post for the definition). We encountered this in Subtract Square when we were writing specific methods of Subtract Square in its individual Subclass.
Here's an example of how Inheritance and Subclasses came into play in our assignment:
We had to make a subclass for the general class because the general class is vague enough that we have to refer to a subclass for specific games, situations, etc. Like our assignment, we made a general class of any particular Game, but what if we wanted to play a specific game? We use Subclasses because every game has different rules and in these subclasses we can use inheritance or override these methods to correspond to the specific game we wanted to play, in this case Subtract Square:
from game_state import GameState
from subtract_square_move import SubstractSquareMove
from math import sqrt
from random import Randint
#This is a crucial step, not importing causes Errors and will not be
#able to inherit from your general class if you did not import it
class SubstractSquareState(GameState):
#General structure of subclass referred to previous post
#"class SubclassName(ClassName):"
'''The state of a Subtract Square game
current_total: int --- total to be subtracted from
'''
So above shows an example of how we created a Subclass in our Assignment Subtract Square. Next is how does this Subclass use Inheritance. Inheritance can be represented in the __init__ method such as so:
def __init__(self, p, interactive=False, current_total=0):
'''(SubstractSquareState, int, str) -> NoneType
(insert docstring here)
'''
GameState.__init__(self, p)
#Notice that I cut out most of the code in __init__
#I just wanted to emphasize the Inheritance part of this code
So the code above that is in the Subclass SubtractSquareState shows inheritance in its __init__method. In this example, it is inheriting GameState and also adding another argument 'p'. This causes the Subclass to inherit everything the general class has but also adding any argument the programmer feels is appropriate for this specific game that is not in the general class because it is not general enough for each game to use.
I hope these examples are relatable and explain these major concepts of OOP that we've come across in this class. If you have any further suggestions, commentary, or opinions, feel free to comment. Also refer back to my post which is linked at the beginning of this post to get the general idea and definitions of these concepts. Happy Programming Sloggers!
Saturday, 28 February 2015
Thursday, 12 February 2015
OOP, there it is! OOP, there it is!
(Haha, clever name I know, trying to make SLOGs a little more interesting... failed. Anyway, if you didn't get it.. forget it.)
Disclaimer: I'm lame, I know.
So far under the OOP umbrella that we have learned, we learned the concepts of defining classes and subclasses, using inheritance...etc which are the basic concepts before you write any code. The key concepts that we covered thus far are:
"""Class docstring
"""
#followed by methods your class wants to cover as well as __init__, __str__, __repr__... etc
""" Subclass doctstring, don't forget to mention that
it is inherited"""
def __init__(self, attribute, attribute):
"""
"""
ClassName.__init__(self, attribute, attribute)
#and assign anything else you need in this
subclass that is not in the superclass
I think these are the two main concepts of Object Oriented Programming that we have covered so far. We've worked with a lot of problem solving involving these concepts and had thorough practice in them. Also, I think going over the assignment and doing all the labs were a great practice in understanding these concepts! Also, this website has helped clarify any confusion I had with OOP and also looking back at the Proff's slides and hope this will help you as well. (Link here!) Cheers!
Edit: Here is a continuation of this post if you want to see these concepts come into play in problems that we've encountered in class to get a better grasp of these definitions and ideas. (Link to Examples Here!)
Disclaimer: I'm lame, I know.
Anyway, on to the topic. This week we're summarizing what we know about Object-Oriented Programming (OOP) concepts. First of all, OOP is a programming language model organized around objects rather than "actions" and data rather than logic. (Definition from here). For those who want to clarify what it is. Basically, the difference is that in logic, it takes an input, processes it and produces an output of data. The material we've mostly covered so far in CSC148 is all about OOP. Where we take objects and we want to manipulate them rather than the logic manipulating them. The object represents the state and behaviour.
So far under the OOP umbrella that we have learned, we learned the concepts of defining classes and subclasses, using inheritance...etc which are the basic concepts before you write any code. The key concepts that we covered thus far are:
- Class: which is an object that defines a set of attributes that characterize any object of the class
- Class statement is written as:
"""Class docstring
"""
#followed by methods your class wants to cover as well as __init__, __str__, __repr__... etc
- We also had some practice in creating instance objects, and accessing attributes in classes
- Inheritance: allows us to add extensions of existing classes to a new class which is the Subclass
- The subclass is the child class where as the existing class (as described above) is the parent class
- This is useful when you want to make a general class and apply it to different circumstances of that class. For example, if you want to make a general class Shape and make subclasses of specific shapes such as Square, Rectangle ..etc. This could be done by using inheritance and/or over-riding methods in the class itself.
- Inheritance in subclasses look like this:
""" Subclass doctstring, don't forget to mention that
it is inherited"""
def __init__(self, attribute, attribute):
"""
"""
ClassName.__init__(self, attribute, attribute)
#and assign anything else you need in this
subclass that is not in the superclass
I think these are the two main concepts of Object Oriented Programming that we have covered so far. We've worked with a lot of problem solving involving these concepts and had thorough practice in them. Also, I think going over the assignment and doing all the labs were a great practice in understanding these concepts! Also, this website has helped clarify any confusion I had with OOP and also looking back at the Proff's slides and hope this will help you as well. (Link here!) Cheers!
Edit: Here is a continuation of this post if you want to see these concepts come into play in problems that we've encountered in class to get a better grasp of these definitions and ideas. (Link to Examples Here!)
Saturday, 7 February 2015
Recursion, smurshion..
Ahhhh Recursion, the most interesting thing i've come across in 148 thus far. Looking at it for the first time was clearly A LOT of confusion. Thoughts of "Why is it calling itself in it's own body, what does it even do, can it even do that?" and being confused for about a week until I woke up one morning, and i'm not kidding you when I say I had an epiphany after I dreamt about trying to solve a recursion problem in my sleep. Haha I'm laughing as I type this because this seriously sounds insane, even to me. No joke, I can kind of explain this phenomena by something I learned while studying for my Psychology midterm on Tuesday. It's called Problem-solving theory where someone solves the problems that they encounter when they're awake in their sleep and dreams.
Anyway, that's a fun story for a SLOG but anyway recursion is a concept where it involves breaking down bigger problems to smaller problems and smaller problems until it can be solved trivially. After thinking about it for a while on my train ride to my 11AM 148 lab after my 'dream', I was envisioning how Python reads this code and what i've come up with is that it checks the input that you give it and sees if its the type of object that "isinstance" wants it to be (well in the case of the recursion problems that we've worked on in the lab). If it is not the specified object type that "isinstance" wants and is checking for, it goes to the else statement and returns whatever it wants it states to return. But if it is, it goes to a return statement that involves a list comprehension where it loops over that list and calls the method again until each nested list is computed and until it can be solved trivially which is pretty neat. Well this is my understanding of how to trace the code.
Tracing a bunch during the lab was really really helpful in understanding how Python goes through a nested list using recursion but practicing it over and over and tracing it, you really get the hang of it. I'm sure I need more practice in understanding how it loops over as I probably messed up my test because I got confused at one point but hey, i'll learn from my mistake. I also need to practice more because i'm sure we're going to have to write a bunch of them soon and it's a bittersweet feeling. Bitter because i'm sure it'll be tough in the beginning but sweet because who doesn't love problem solving questions and it's very useful especially for calculating something in nested lists and other things! Yay, more programming fun.
P.S. Recursion is pretty difficult to understand but when I was stuck, I stumbled upon this website (here) and really helped me understand the concept a little more. Hope this helps you as well! Cheers xoxo.
Anyway, that's a fun story for a SLOG but anyway recursion is a concept where it involves breaking down bigger problems to smaller problems and smaller problems until it can be solved trivially. After thinking about it for a while on my train ride to my 11AM 148 lab after my 'dream', I was envisioning how Python reads this code and what i've come up with is that it checks the input that you give it and sees if its the type of object that "isinstance" wants it to be (well in the case of the recursion problems that we've worked on in the lab). If it is not the specified object type that "isinstance" wants and is checking for, it goes to the else statement and returns whatever it wants it states to return. But if it is, it goes to a return statement that involves a list comprehension where it loops over that list and calls the method again until each nested list is computed and until it can be solved trivially which is pretty neat. Well this is my understanding of how to trace the code.
Tracing a bunch during the lab was really really helpful in understanding how Python goes through a nested list using recursion but practicing it over and over and tracing it, you really get the hang of it. I'm sure I need more practice in understanding how it loops over as I probably messed up my test because I got confused at one point but hey, i'll learn from my mistake. I also need to practice more because i'm sure we're going to have to write a bunch of them soon and it's a bittersweet feeling. Bitter because i'm sure it'll be tough in the beginning but sweet because who doesn't love problem solving questions and it's very useful especially for calculating something in nested lists and other things! Yay, more programming fun.
P.S. Recursion is pretty difficult to understand but when I was stuck, I stumbled upon this website (here) and really helped me understand the concept a little more. Hope this helps you as well! Cheers xoxo.
Subscribe to:
Posts (Atom)