diff options
Diffstat (limited to 'mit/oop')
| -rw-r--r-- | mit/oop/fractions.py | 80 | ||||
| -rw-r--r-- | mit/oop/mine.py | 21 | ||||
| -rw-r--r-- | mit/oop/myclasses.py | 71 |
3 files changed, 172 insertions, 0 deletions
diff --git a/mit/oop/fractions.py b/mit/oop/fractions.py new file mode 100644 index 0000000..46f62a8 --- /dev/null +++ b/mit/oop/fractions.py @@ -0,0 +1,80 @@ +#!/usr/bin/python3 + +# Example of class usage in python. + +# Let's define a new class to represent fraction numbers + + +class fraction(object): + def __init__(self, numer, denom): + self.numer = numer + self.denom = denom + + def __str__(self): + return str(self.numer) + ' / ' + str(self.denom) + + + # Getters definition + # We almost never want to directly manipulate the data inside objects, + # so, we should (almost) always define getters and setters methods for data + # manipulation inside a class + + def getNumer(self): + return self.numer + + def getDenom(self): + return self.denom + + def __add__(self, other): + new_numer = (self.getNumer() * other.getDenom()) + \ + (other.getNumer() * self.getDenom()) + new_denom = (self.getDenom() * other.getDenom()) + + return fraction(new_numer, new_denom) + + def __sub__(self, other): + new_numer = (self.getNumer() * other.getDenom()) - \ + (other.getNumer() * self.getDenom()) + new_denom = (self.getDenom() * other.getDenom()) + + return fraction(new_numer, new_denom) + + # Return the float representation of the fraction + def convert(self): + return self.getNumer() / self.getDenom() + + +# Set example + +# Create a class to represent a set of numbers which are not allowed to be +# repeated + +class Set(object): + + def __init__(self): + self.set = [] + + def insert(self, nval): + if nval not in self.set: + self.set.append(nval) + else: + raise ValueError(str(nval) + " is already in the set") + + def remove(self,nval): + try: + self.set.remove(nval) + except: + raise ValueError(str(nval) + " not found!") + + def member(self, nval): + return nval in self.set + + def __str__(self): + self.set.sort() + + result = '' + + for i in self.set: + result += str(i) + ', ' + + return '{' + result[:-2] + '}' diff --git a/mit/oop/mine.py b/mit/oop/mine.py new file mode 100644 index 0000000..6908a11 --- /dev/null +++ b/mit/oop/mine.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 + +class Mine(object): + def __init__(self, deep): + self.deep = deep + @property + def deep(self): + if self.__deep > 1000: + return 1000 + return self.__deep + @deep.setter + def deep(self, deep): + if deep > 2000: + raise ValueError(str(deep) + " Out of bounds!") + elif deep <= 0: + self.__deep = 0 + else: + self.__deep = deep + + def __str__(self): + return str(self.deep) diff --git a/mit/oop/myclasses.py b/mit/oop/myclasses.py new file mode 100644 index 0000000..9dc57bc --- /dev/null +++ b/mit/oop/myclasses.py @@ -0,0 +1,71 @@ +#!/usr/bin/python3 + +# class <name>(parent) +# Here object is the 'global' parent class +# Parent classes are AKA superclass +class Coordinate(object): + + # __init__ method, is the method called when + # an instance of this class is initialized + # 'self', is just python's formality to point + # an object data and/or object's method to itself. + # any other name can actually be used. + + # Python will always pass the object itself as the first argument (hidden) + def __init__(self, x, y): + self.x = x + self.y = y + + + def distance(self, other): + x_dist = (self.x - other.x) ** 2 + y_dist = (self.y - other.y) ** 2 + return (x_dist + y_dist) ** 0.5 + + + # Special method for use when print(object) is called, it MUST return a + # string + def __str__(self): + return "<" + str(self.x) + "," + str(self.y) + ">" + + # Operator Overloading + def __add__(self, other): # self + other + # Returns a new coordinate with the sum + return Coordinate(self.x + other.x, self.y + other.y) + def __sub__(self, other): # self - other + # Returns a new coordinate with the subtraction + return Coordinate(self.x - other.x, self.y - self.x) +# def __eq__(self,other) # self == other +# def __lt__(self,other) # self < other +# def __len__(self) # len(self) +# def __str__(self) # print(self) + + +# Notice (as in the the overloading example above), that we can make the Class +# (or the instanced objects), to return new instances of the class + + +# Example usage: + +# Notice we don't need to pass the 1st (self) argument. Since the first argument +# is the object itself, python pass it automatically when instantiating the +# object. + +# When you define a method, python will ALWAYS pass the actual object as the +# first argument for that method, and for convention, we use 'self' as name + +a = Coordinate(3, 4) +b = Coordinate(0, 0) + +# Remember, the first argument (the object itself), is passed automaticaaly +a.distance(b) + +# We can use the class itself to call the methods, instead of an instance. But +# in this case, we need to provide the "self" argument, once we have no +# instanced object for python to pass it automatically +Coordinate.distance(a, b) + + +# isinstance() is a global function which return True/False if the object is an +# instance of t he specific class +isinstance(a, Coordinate) |
