Monday, January 20, 2014

Week One

Object-Oriented Programming:

Let's begin by creating our very own class. Something simple such as co-ordinates on the (x, y) plane. Thinking back to CSC108, I would do something like this:

class Point(object):
    """n-dimensional point
    """
 def __init__(self, x, y):
        """ (Point, float, float) - > NoneType
        Create a new point at x, y.
        """
    self.x = x
    self.y = y

Sweet. Now let's see how far it is from the origin by creating a new function.

def distance(self, x, y):
    """ Compute distance from the origin """
    return ((self.x ** 2) + (self.y ** 2)) ** 0.5

What if we have three points such as the Cartesian Plane (x, y, z). I guess we can go back and and self.z = z in the __init__ method. What if there is an infinite number, this seems like it's going to get painfully tedious. There must be another way...

class Point(object):
    """n-dimensional point
    """
    def __init__(self: "Point", coord: [float, ]) -> None:
        self.coord = [float(x) for x in coord]

Mind-blowing. Looks like gibberish at the moment.  ``Point`` is a type class that refers to self and [float, ] is a list of n variables.Using a for loop in the initializer, this lets us have a list of as many variables as we please.

list comprehensions --- [<expression> for x in iterable]

The new type contract helps us avoid __init__(self, x, y, z, ..., N):  Also, I'm not sure why but in the second contract, I get what looks like an invalid syntax error in WingIDE in the init method. Under the "-> None" , it looks something like this:

def __init__(self: "Point", coord: [float, ]) -> None:





No comments:

Post a Comment