Last active
August 4, 2017 19:19
-
-
Save lx-88/69e852ace824f7cd3e49641c0a2a2d6f 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
class Point(object): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
# See how I dont have to define these above but should define them in init? | |
self.secret = 'hello Burke' | |
self.message = None | |
self.model_results = list() | |
def __repr__(self): | |
return "Point<{0}, {1}>".format(self.x, self.y) | |
def do_something(self, message): | |
self.message = message | |
print("If this did something that would be cool instead of just update message. self.message is {0}".format( | |
self.message)) | |
def run_model(self): | |
for i in range(0, 5): | |
self.model_results.append(i) | |
print(" .. {0}".format(i)) | |
class Point3D(Point): | |
def __init__(self, x, y, h): | |
super(Point3D, self).__init__(x, y) | |
self.h = h | |
def do_something(self): | |
print("We completly overwrote this function") | |
return None | |
def __repr__(self): | |
return "Point3D<{0}, {1}, {2}>".format(self.x, self.y, self.h) | |
@property | |
def sum_coords(self): | |
print("We can also completly essentially add virtual fields") | |
return self.x + self.y + self.h | |
if __name__ == "__main__": | |
print("Point 1") | |
pt1 = Point(10.0, 20.0) | |
print("... but we can also define another obj of the Point type") | |
pt2 = Point(34.0, 100.0) | |
print("See how the values of pt1.x != pt2.x ? That's because they are different instances of the same class") | |
print("pt1.x: {0}".format(pt1.x)) | |
print("pt1.secret: {0}".format(pt1.secret)) | |
print("pt2.x: {0}".format(pt2.x)) | |
print("pt2.secret: {0}".format(pt2.secret)) | |
print("But I can change values") | |
pt2.secret = 'Hello 12222' | |
print("pt2.secret is now: {0}".format(pt2.secret)) | |
# Value of model_results before we run run_model() | |
print("Value of model_results BEFORE we run run_model()") | |
print(pt1.model_results) | |
print("") | |
# Classes can do stuff | |
print("run the model:") | |
pt1.run_model() | |
print("Value of model_results AFTER we run run_model()") | |
print(pt1.model_results) | |
# Repr allows you define what is shown when the obj gets printed | |
print("We can also use the __repr__ definition to control how the object prints or is represented as a string") | |
print(pt1) | |
pt2 = Point(34.0, 100.0) | |
# Ok, now lets try that subclass | |
pt13d = Point3D(10.0, 20.0, 10.0) | |
print(pt13d) | |
print("pt13d.secret: {0}".format(pt13d.secret)) | |
pt13d.do_something() | |
print("now run the model, its still there") | |
pt13d.run_model() # See how this still works because the Point3d is subclasses from Point | |
print("p13d.sum_coords: {0}".format(pt13d.sum_coords)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment