Created
January 30, 2019 21:12
-
-
Save emilyhorsman/1f0bccad816893c3ed51edf9e1264919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Abstract Data Type | |
class PointT: | |
def __init__(self, x, y): | |
self.__point = (x, y) | |
# sum : PointT -> R | |
# sum is an instance method. | |
# It acts on one instance. | |
# In Python we call an instance an object. | |
# An object from the class PointT is an object | |
# of type PointT. | |
# p : PointT | |
def sum(self): | |
# self.__point is the state variable 'point'. | |
return self.__point[0] + self.__point[1] | |
p = PointT(1, 1) | |
q = PointT(2, 2) | |
print('p sum:', p.sum()) # 1 + 1 = 2 | |
print('q sum:', q.sum()) # 2 + 2 = 4 | |
# Abstract Object | |
# You might here "singleton" | |
class Data: | |
Points = [] | |
def __init__(self): | |
return None | |
# "staticmethod" is called in Python "decorator" | |
# You can recognize by its @ | |
@staticmethod | |
def first_sum(): | |
return Data.Points[0].sum() | |
# NOT GOING TO DO THIS: | |
#d = Data() | |
#another_d = Data() | |
Data.S = [p, q] | |
d = Data() | |
d.x = 4 | |
print(d.x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment