Skip to content

Instantly share code, notes, and snippets.

@emilyhorsman
Created January 30, 2019 21:12
Show Gist options
  • Save emilyhorsman/1f0bccad816893c3ed51edf9e1264919 to your computer and use it in GitHub Desktop.
Save emilyhorsman/1f0bccad816893c3ed51edf9e1264919 to your computer and use it in GitHub Desktop.
# 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